Python中的装饰器

  • @property 让类函数能像类变量一样操作。
  • @classmothod类函数,属于整个类,类似于C++/JAVA中的静态函数。类方法有类变量cls传入,从而可以用cls做一些相关的处理。子类继承时,调用该类方法时,传入的类变量cls是子类,而非父类。既可以在类内部使用self访问,也可以通过实例、类名访问。即在类的函数前加@classmethod属性,函数第一个参数为cls类,表示该函数是类的方法,而不需要绑定具体实例。
    在类方法里面可以调用类的属性,并且在类调用该函数时,会将自身作为第一个参数传入,类方法不能访问实例。实例可以调用类方法。
  • @staticmethod 将外部函数集成到类体中,既可以在类内部使用self访问,也可以通过实例、类名访问。基本上等同于一个全局函数。
  • 使用@staticmethod或@classmethod,就可以不需要实例化,直接类名.方法名()来调用。这有利于组织代码,把某些应该属于某个类的函数给放到那个类里去,同时有利于命名空间的整洁。
  • staticmethod基本没用只是为了整合到class里面方便使用

A staticmethod is a method that knows nothing about the class or instance it was called on. It just gets the arguments that were passed, no implicit first argument. It is basically useless in Python -- you can just use a module function instead of a staticmethod.

  • 目的是为了直接使用类就可以调用这个方法,不用实例化才能调用。

A classmethod, on the other hand, is a method that gets passed the class it was called on, or the class of the instance it was called on, as first argument. This is useful when you want the method to be a factory for the class: since it gets the actual class it was called on as first argument, you can always instantiate the right class, even when subclasses are involved. Observe for instance how dict.fromkeys(), a classmethod, returns an instance of the subclass when called on a subclass:

class room:
    tag=1
    def __init__(self, name,length,width,height):
        self.name=name
        self.length=length
        self.width=width
        self.height=height
    @property  #通过类提供的property对求面积的函数进行封装
    def square(self):
        s=self.length*self.width
        print('%s的面积为%s'%(self.name,s))
    @classmethod
    def volumn(cls,length,width,height):  #第一个参数为cls,即类
        v=length*width*height
        print('访问类的属性%s'%cls.tag)  #类方法可以访问类的属性
        print('类%s的体积为%s'%(cls,v))
    @staticmethod
    def haha(x,y):  #静态方法既没有类cls参数,也没有实例self参数,因此不能访问类和实例的属性
        print('From staticmethod,%s and %s is haha'%(x,y))
    def hahahaha(x,y): #可以这样定义普通方法,但不建议这样使用,没有意义
        print('From common method,%s and %s is hahahaha' % (x, y))
room1=room('room1',10,13,3)
room1.square #直接调用求面积的函数方法就可以执行函数,而不需要加()
room.volumn(10,13,3)  #类调用类方法时,将类自身作为第一个参数传入
room.haha('Jane','Alan')
room.hahahaha('Jane','Alan')

# 执行结果:
# room1的面积为130
# 访问类的属性1
# 类<class '__main__.room'>的体积为390
# From staticmethod,Jane and Alan is haha
# From common method,Jane and Alan is haha
  • @classmothod和staticmethod的区别:staticmethod不需要表示自身对象的self和自身类的cls参数,就跟使用函数一样。
  • classmethod必须使用类对象作为第一个参数,而staticmethod则可以不传递任何参数。
  • 例子
class A(object):  
    bar = 1  
    def foo(self):  
        print 'foo'  
 
    @staticmethod  
    def static_foo():  
        print 'static_foo'  
        print A.bar  
 
    @classmethod  
    def class_foo(cls):  
        print 'class_foo'  
        print cls.bar  
        cls().foo()  
  
A.static_foo()  
A.class_foo()  
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • python中的装饰器 1. @property ['prɑpɚti] @property装饰器就是负责把一个方法...
    yuanquan521阅读 767评论 0赞 0
  • 1.列表解析式和字典解析式 列表解析式语法 [expr for value in collection if...
    MononokeHime阅读 2,077评论 0赞 8
  • 几缕相思, 些许赞叹, 几多恭敬, 少许执着, 多些欢喜, 少量悭吝, 几多智慧, 一碗赤诚, 此粥名为:离苦得乐腊八粥
    冰释之阅读 314评论 0赞 2
  • 16年在深圳过年,我为数不多,在房间里念经,念《佛说大乘金刚经果论》。爷爷推门进来,安静听我念经。爷爷说,他听不懂...
    大雾见性阅读 285评论 0赞 0
  • 文/素白 我尝试以最大的幸运在月亮瘦成一把刀刃时将麦子的故事 托付给稻草人从不掉泪的眼睛 又怎会知道 除了喜悦...
    素白_阅读 2,120评论 23赞 66

友情链接更多精彩内容