zl程序教程

您现在的位置是:首页 >  其他

当前栏目

class method & static method 类的装饰器函数

amp 函数 Class static method 装饰
2023-09-11 14:16:16 时间

  

class Mse:
   @classmethod
  def __new__(cls,*args,**kwargs):
    pass @classmethod def class_method(cls): print(
'class={0.__name__} ({0})'.format(cls)) cls.HEIGHT=88 @staticmethod def static_method(): print(Mse.HEIGHT) Mse.class_method()   Mse.static_method()
print(Mse.__dict__)

 

def setnameproperty(name):
    def wrapper(cls):
        cls.NAME=name
        return cls
    return wrapper
@setnameproperty('vbn')
class MyXiv:
    pass
print(MyXiv.NAME)
print(MyXiv.__dict__)

MyXiv仍然指向同一内存地址

MyXiv=wrapper(MyXiv)

 

class Cin:
    def __init__(self,name,age=19):
        self.bname=name
        self._bage=age
    def _getname(self):
        return self.bname
    def __getage(self):
        return self._bage
tom=Cin('tom')
print(tom._getname())
# print(tom.__getage())
print(tom.__dict__)
print(tom.__class__.__dict__)
print(tom._Cin__getage())