还没写完
classmethod(function)
详细用法请参见我之前翻译的文章:
getattr(object, name[, default])
hasattr(object, name[, default])
class Demo:
def __init__(self, i):
self.i = i
self.x = "xxx"
self.y = "yyy"
self.z = "zzz"
def __str__(self):
return str(self.i)
def hello(self):
print("hello " + self.__str__())
d = Demo(22)
print(getattr(d, "__init__"))
print(getattr(d, "i"))
print(getattr(d, "x"))
print(getattr(d, "y"))
print(getattr(d, "z"))
print(getattr(d, "__str__"))
print(getattr(d, "hello"))
print(hasattr(d, "init")) #不存在init
print(hasattr(d, "i")) #存在i
### 结果:
<bound method Demo.__init__ of <__main__.Demo object at 0x7f5327c74940>>
22
xxx
yyy
zzz
<bound method Demo.__str__ of <__main__.Demo object at 0x7f5327c74940>>
<bound method Demo.hello of <__main__.Demo object at 0x7f5327c74940>>
False
True