class MyClass(object):
def __call__(self, num):
print("in __call__: %s", num)
def __init__(self, num):
self.num = num
def __str__(self):
print("in __str__")
return "test"
def __repr__(self):
print("in __repr__")
return "test2"
a = MyClass(4)
print a
print a(75)
a(80)
运行结果
import sys, os
class UrlChain(object):
def __init__(self, path=''):
print("in __init__")
self._path = path
def __getattr__(self, guan):
print("in __getattr__")
print("in __getattr__: %s" % guan)
return UrlChain('%s/%s' % (self._path, guan))
def __str__(self):
print("in __str__")
return self._path
def __call__(self, index):
print("in __call__")
return UrlChain('%s/%s' % (self._path, index))
class CephManager(object):
base_url = UrlChain('v1')
def func1(self):
print self.base_url.clusters('JeffreyGuan')
cm = CephManager()
cm.func1()