improve your python code(8)
来源:互联网 发布:美工学徒 编辑:程序博客网 时间:2024/05/19 00:38
1. [] {} ().用列表解析器代替for遍历
理由:
高校
2. 函数传参既非传引用也非传值
a = 5 # 开辟一个内存存放5,将a这个标签贴在这个内存上b = a # 将b这个标签也贴在刚才的那个内存上b = 7 # 开辟一个内存存放7,将b这个标签贴在这个内存上,刚才的那个b被覆盖。
3. *args,**kwargs
4. staticmethod和classmethod
首先要明白,什么是实例方法、静态方法和类方法:
class Demo(object): def instance_method(self, your_para): """ this is an instance_method you should call it like the follow: a = Demo() a.instance_method(your_para) plus: in python, we denote 'cls' as latent para of Class while 'self' as latent para of the instance of the Class :param your_para: :return: """ print("call instance_method and get:", your_para) @classmethod def class_method(cls, your_para): """ this is a class_method you can call it like the follow: method1: a = Demo() a.class_method(your_para) method2: Demo.class_method plus: in python, we denote 'cls' as latent para of Class while 'self' as latent para of the instance of the Class :param your_para: :return: """ print("call class_method and get:", your_para) @staticmethod def static_method(your_para): """ this is a static_method and you can call it like the methods of class_method :param your_para: :return: """ print("call static_method and get:", your_para)
- 虽然类方法在调用的时候没有显式声明cls,但实际上类本身是作为隐含参数传入的。这就像实例方法在调用的时候也没有显式声明self,但实际上实例本身是作为隐含参数传入的。
- 对于静态函数,我们一般把与类无关也与实例无关的函数定义为静态函数。例如入口检查的函数就最好定义成静态函数。
- 类方法的妙处:
1,在继承中的作用:
class Fruit(object): total = 0 # 这是一个类属性 @classmethod def print_total(cls): print('this is the ', cls, '.total:', cls.total, ' and its id: ', id(cls.total)) # cls是类本身,打印类属性total的值 print('this is the Fruit.total:', Fruit.total, 'and its id: ', id(Fruit.total)) print("=======================") @classmethod def set(cls, value): cls.total = valueclass Apple(Fruit): passclass Orange(Fruit): passapp1 = Apple()app1.set(10)app1.print_total()Apple.print_total()Fruit.set(2)app1.print_total()Fruit.print_total()"""output:this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264this is the Fruit.total: 0 and its id: 1355200944=======================this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264this is the Fruit.total: 0 and its id: 1355200944=======================this is the <class '__main__.Apple'> .total: 10 and its id: 1355201264this is the Fruit.total: 2 and its id: 1355201008=======================this is the <class '__main__.Fruit'> .total: 2 and its id: 1355201008this is the Fruit.total: 2 and its id: 1355201008======================="""
0 0
- improve your python code(8)
- improve your python code(1)
- improve your python code(2)
- improve your python code(3)
- improve your python code(4)
- improve your python code(5)
- improve your python code(6)
- improve your python code(7)
- improve your python code(9)
- improve your python code(10)
- improve your python code(11)
- improve your python code(12)
- improve your python code(13)
- Improve Your Python: 'yield' and Generators Explained
- Improve Your Python: 'yield' and Generators Explained
- How to improve quality and syntax of your Android code
- Teaching course1 : How to improve your code quality
- 转-Code-Switching-to-Improve-Your-Writing-and-Productivity
- 本地nodejs服务(笔记)
- 查找算法的时间复杂度
- 12th湖南省赛 A-2016(同余定理)
- NDk的初使用
- Html中typeof的用法
- improve your python code(8)
- SpringMVC运行流程图
- python实现pcd转txt
- 给IT大学生的10个忠告
- GDOI2017总结
- Android 腾讯X5浏览器 初始化不成功的原因
- 数据访问层
- Spark 以及 spark streaming 核心原理及实践
- 我要背单词