美文网首页
Python实例方法 静态方法 类方法

Python实例方法 静态方法 类方法

作者: Treehl | 来源:发表于2018-06-26 14:03 被阅读0次

    @staticmethod和@classmethod的区别

    • class 类
    • @staticmethod
    • @classmethod

    首先创建一个类,如果想要调用Student类中的方法get_score(),就需要先创建类的实例,然后再用类的对象再去调用方法

    In [3]: class Student():
       ...:     def __init__(self, name, score):
       ...:         self.name = name
       ...:         self.score = score
       ...:     def get_score(self):
       ...:         print('%s get %s ' % (self.name, self.score))
       ...:
       ...:
    
    In [4]: student = Student('treehl', 100)
    
    In [5]: student.get_score()
    treehl get 100
    
    

    @staticmethod和@classmethod一个是静态方法,另一个是类的方法,两个装饰器的作用都可以使类不必再创建实例,直接用类来调用方法(类名.方法())

    它们使用上的区别

    • @staticmethod不需要传递self,也不需要cls参数,就跟使用函数一样(类名.方法()或类名.属性名)
    • @classmethod也不需要像实例方法一样要传递self,但它需要cls参数(cls.类名()或cls.方法()或cls.属性())

    要理解这些,首先需要理解类属性和实例属性的区别

    
    In [6]: class A():
       ...:     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()
       ...:
    
    In [7]: A.static_foo()
    static_foo
    1
    
    In [8]: A.class_foo()
    class_foo
    1
    foo
    

    欢迎访问Treehl的博客
    Github

    相关文章

      网友评论

          本文标题:Python实例方法 静态方法 类方法

          本文链接:https://www.haomeiwen.com/subject/ymzpyftx.html