__init__()和__new__()
# __init__方法用来初始化实例后的对象
class A:
def __init__(self, name):
self.name = name
# __new__方法构造方法是实例化第一个执行的方法,一般不会去重写
class A(object):
def __new__(cls, *args, **kwargs): # 必须有一个参数cls类本身
print "new %s" % cls
return object.__new__(cls, *args, **kwargs) # 调用object的new来处理
__slots__
# __slots__显示类实例动态绑定
class A(object):
__slots__ = ('name', 'age')
a = A()
a.name = 'Tom'
a.age = 22
a.score = 60 # 报错 AttributeError异常
__len__()
# __len__()如果一个类可获取有多少个元素,就要用len()函数获取,用len()函数时这个类中必须有__len__()特殊方法来返回长度
class A(object):
def __init__(self, *args):
self.names = args
def __len__(self):
return len(self.names)
__str__()和__repr__()
# __str__()和__repr__()用来直接访问类实例的显示,前者是给用户显示的,后者是程序调试显示用的
class A(object):
def __init__(self, name):
self.name = name
a = A('Tom')
print(a) # <__main__.A object at 0x000001F8DE4CB0F0> 不友好显示
# 改写类A
class A(object):
def __init__(self, name):
self.name = name
def __str__(self):
return 'A object (name: %s)' % self.name
__repr__ = __str__ # __repr__偷懒做法
a = A('Tom')
print(a) # A object (name: Tom) 一目了然
__iter__()和__next__()
# 如果一个类想被用于for ... in循环,类似list或tuple那样,就必须实现一个__iter__()方法,
# 该方法返回一个迭代对象,然后,Python的for循环就会不断调用该迭代对象的__next__()方法拿到循环的下一个值,
# 直到遇到StopIteration错误时退出循环
class Fib(object):
def __init__(self):
self.a, self.b = 0, 1 # 初始化两个计数器a,b
def __iter__(self):
return self # 实例本身就是迭代对象,故返回自己
def __next__(self):
self.a, self.b = self.b, self.a + self.b # 计算下一个值
if self.a > 100000: # 退出循环的条件
raise StopIteration()
return self.a # 返回下一个值
__getitem__()、__setitem__()、__delitem__()
# __getitem__ 让对象可像list一样用索引取元素, __setitem__让对象像list一样赋值,__delitem__删除一个元素
class Fibo(object):
def __getitem__(self, n): # n是索引
a, b = 1, 1
for i in range(n):
a, b = b, a + b
return a
a = Fibo()
a[0] # 1
# 切片
class Fib(object):
def __getitem__(self, n):
if isinstance(n, int): # n是索引
a, b = 1, 1
for x in range(n):
a, b = b, a + b
return a
if isinstance(n, slice): # n是切片
start = n.start
stop = n.stop
if start is None:
start = 0
a, b = 1, 1
L = []
for x in range(stop):
if x >= start:
L.append(a)
a, b = b, a + b
return L
__getattr__()
# __getattr__() 当调用类中不存在的属性触发
class Student(object):
def __init__(self):
self.name = 'Michael'
def __getattr__(self, attr):
if attr=='score':
return 99 # 也可返回函数 return lambda: 25
raise AttributeError('\'Student\' object has no attribute \'%s\'' % attr) # 自定义抛异常
a = Student()
a.score # 99
a.score() # 25
__call__()
# __call__()用于直接调用实例化对象
class A(object):
def __init__(self, name):
self.name = name
def __call__(self):
print('My name is %s.' % self.name)
a = A('Tom')
a() # My name is Tom