Amae 2022-06-23 19:42:53 阅读数:595
Two ways to add attributes to a class
The first one is : Through functions
def add_name(name,cls):
cls.NAME = name
class Person:
AGE = 3
add_name(name="TOM",cls=Person)
print(Person.NAME)
result :
TOM
The second is realized by decorator
def setnamepropery(name):
def wrapper(cls):
cls.NAME=name
return cls
return wrapper
@setnamepropery('TOM') #setnamepropery=setnamepropery(Person)
class Person:
AGE =3
print(Person.NAME)
result
TOM
版权声明:本文为[Amae]所创,转载请带上原文链接,感谢。 https://pythonmana.com/2022/174/202206231839325963.html