[oracle@node01 0914]$ cat a1.py
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def query(self,friend):
print self.name
print self.gender
self.friend=friend
print self.friend
h=Person('aa','bb')
h.query('cc')
[oracle@node01 0914]$ python a1.py
aa
bb
cc
[oracle@node01 0914]$
[oracle@node01 0914]$ cat a2.py
class Person(object):
def __init__(self, name, gender):
self.name = name
self.gender = gender
def __call__(self,friend):
print self.name
print self.gender
self.friend=friend
print self.friend
p=Person('Bob', 'male')
p('Tim')
[oracle@node01 0914]$ python a2.py
Bob
male
Tim
一个类实例也可以变成一个可调用对象,只需要实现一个特殊方法__call__()。