zoukankan      html  css  js  c++  java
  • @property、@staticmethod、@classmethod装饰器

    @property装饰器放置在类中的无需带参数传递的函数上一行,表示该函数为类的一个属性,调用方法为:类对象.函数名

    eg:

    #!/usr/bin/python
    
    class test():
        def __init__(self,value):
            self.value=value
        @ property
        def a(self):
            print self.value
            print("in the a")
    b=test("12")
    b.a
    
    [root@jht pyscript]# python st_property_py 
    12
    in the a

    @staticmethod静态方法装饰器,放置在类中的无需带参数传递的函数上一行,表示该函数为类的一个静态方法。

    @classmethod类方法装饰器,被装饰的函数是被类调用的。

    [root@jht pyscript]# cat st_property_py 
    #!/usr/bin/python
    class t1:
        x=1
        @classmethod
        def test(va):
            print(va,va.x)
    t1.test() class t2(t1): x=2 t2.test()
    [root@jht pyscript]# python st_property_py (<class __main__.A at 0x7f066642b530>, 1) (<class __main__.B at 0x7f066642b668>, 2)

    ****静态方法和类方法虽然是给类准备的,如果实例去用,不会报错,只不过实例去调用的时候容易让人混淆*****

  • 相关阅读:
    day25:接口类和抽象类
    vue1
    How the weather influences your mood?
    机器学习实验方法与原理
    How human activities damage the environment
    Slow food
    Brief Introduction to Esports
    Massive open online course (MOOC)
    Online learning in higher education
    Tensorflow Dataset API
  • 原文地址:https://www.cnblogs.com/st12345/p/9083802.html
Copyright © 2011-2022 走看看