>>> class people(object):
... age = 10
... def __init__(self, name):
... self.name = name
... def say(self):
... print 'good bye:%s' % self.name
...
>>> a = people('guo')
>>> a.name
'guo'
>>> a.say()
good bye:guo
>>> a.age
10
类工具
a = people('guo')
>>> a.__dict__ {'name': 'guo'}
>>> a.__class__ <class '__main__.people'>
>>> a.__class__.__name__ 'people'
dir(a) ['__class__', '__dict__', ..., 'age', 'name', 'say']
hasattr(a, 'age')
setattr(a, 'age', 19)
getattr(a, 'age', 0)
类方法--(实例方法 / 静态方法 / 类方法)
class Methods:
def imeth(self, x): print(self, x)
def smeth(x): print(x)
def cmeth(cls, x): print(cls, x)
smeth = staticmethod(smeth)
cmeth = classmethod(cmeth)
上述中:
@staticmethod
def smeth(x): print(x)
def smeth(x): print(x)
smeth = staticmethod(smeth)
@classmethod
def cmeth(cls, x): print(x)
def cmeth(cls, x): print(x)
cmeth = classmethod(cmeth)