Day.16
# __new__是在一个对象实例化时所调用的第一个方法
# 在调用__init__初始化前先调用__new__
class A(object):
def __init__(self,value):
print('into B __init__ ')
self.value = value
def __new__(cls,*args,**kwargs): #__new__至少要有一个参数cls
print('3')
print('4')
return object.__new__(cls) #只有当前类的__new__返回了,当前类的__init__才会进入
class B(A):
def __init__(self,value):
print('5')
self.value = value
def __new__(cls,*args,**kwargs):
print('1')
print('2')
return super().__new__(cls,*args,**kwargs)
#只有当前类的__new__返回了,当前类的__init__才会进入
b = B(10)
1
2
3
4
5
class Dl:
pass
a = Dl()
print(id(a)) # 1689802833096
b = Dl()
print (id(b)) # 1689802830728
#用 __new__实现单例模式
class Dl(object): # 定义一个类做判断
__flag = None
def __new__(self):
if self.__flag is None:
self.__flag = object.__new__(self)
return self.__flag
else:
return self.__flag
a = Dl()
print(id(a)) # 1689803539336
b = Dl()
print (id(b)) # 1689803539336
2__del__(self) 析构器,当一个对象将要被系统回收之时调用的方法。
Python 采用自动引用计数(ARC)方式来回收对象所占用的空间, 当程序中有一个变量引用该 Python 对象时,Python 会自动保证该对象引用计数为 1; 当程序中有两个变量引用该 Python 对象时,Python 会自动保证该对象引用计数为 2, 依此类推,如果一个对象的引用计数变成了 0,则说明程序中不再有变量引用该对象, 表明程序不再需要该对象,因此 Python 就会回收该对象。 大部分时候,Python 的 ARC 都能准确、高效地回收系统中的每个对象。 但如果系统中出现循环引用的情况,比如对象 a 持有一个实例变量引用对象 b, 而对象 b 又持有一个实例变量引用对象 a,此时两个对象的引用计数都是 1, 垃圾回收器就可能没那么快,要等专门的循环垃圾回收器 (Cyclic Garbage Collector)来检测并回收这种引用循环。
class C(object):
def __init__(self):
print('into C __init__')
def __del__(self):
print('into C __del__')
c1 = C()
# into C __init__
c2 = c1
c3 = c2
del c3
del c2
del c1
# into C __del__
3__str__ 和 repr
当你打印一个对象的时候,触发__str__
当你使用%s格式化的时候,触发__str__
str强转数据类型的时候,触发__str__
__repr__是__str__的备胎
有__str__的时候执行_str_
,没有实现_str_的时候,执行__repr__
__repr(obj)__内置函数对应的结果是__repr__的返回值
当你使用%r格式化的时候 触发__repr__
class Cat:
"""定义一个猫类"""
def __init__(self, new_name, new_age):
"""在创建完对象之后 会自动调用, 它完成对象的初始化的功能"""
self.name = new_name
self.age = new_age
def __str__(self):
"""返回一个对象的描述信息"""
return "名字是:%s , 年龄是:%d" % (self.name, self.age)
def __repr__(self):
"""返回一个对象的描述信息"""
return "Cat:(%s,%d)" % (self.name, self.age)
def eat(self):
print("%s在吃鱼...." % self.name)
def drink(self):
print("%s在喝可乐..." % self.name)
def introduce(self):
print("名字是:%s, 年龄是:%d" % (self.name, self.age))
# 创建了一个对象
tom = Cat("汤姆", 30)
print(tom) # 名字是:汤姆 , 年龄是:30
print(str(tom)) # 名字是:汤姆 , 年龄是:30
print(repr(tom)) # Cat:(汤姆,30)
tom.eat() # 汤姆在吃鱼....
tom.introduce() # 名字是:汤姆, 年龄是:30
__add(self, other)__定义加法的行为:+
__sub(self, other)__定义减法的行为:-
mul(self, other)定义乘法的行为:*
truediv(self, other)定义真除法的行为:/
__floordiv__(self, other)定义整数除法的行为://
__divmod__(self, other)定义当被 divmod() 调用时的行为
__divmod__(a, b)把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b)。
__pow__(self, other[, module])定义当被 power() 调用或 ** 运算时的行为
__lshift__(self, other)定义按位左移位的行为:<<
__rshift__(self, other)定义按位右移位的行为:>>
__and__(self, other)定义按位与操作的行为:&
__xor__(self, other)定义按位异或操作的行为:^
__or__(self, other)定义按位或操作的行为:|
反运算魔方方法,与算术运算符保持一一对应,不同之处就是反运算的魔法方法多了一个“r”。
当文件左操作不支持相应的操作时被调用。
__radd__(self, other)定义加法的行为:+
__rsub__(self, other)定义减法的行为:-
__rmul__(self, other)定义乘法的行为:*
__rtruediv__(self, other)定义真除法的行为:/
__rfloordiv__(self, other)定义整数除法的行为://
__rmod__(self, other) 定义取模算法的行为:%
__rdivmod__(self, other)定义当被 divmod() 调用时的行为
__rpow__(self, other[, module])定义当被 power() 调用或 ** 运算时的行为
__rlshift__(self, other)定义按位左移位的行为:<<
__rrshift__(self, other)定义按位右移位的行为:>>
__rand__(self, other)定义按位与操作的行为:&
__rxor__(self, other)定义按位异或操作的行为:^
__ror__(self, other)定义按位或操作的行为:|
这里加数是a,被加数是b,因此是a主动,反运算就是如果a对象的add()方法 没有实现或者不支持相应的操作,那么 Python 就会调用b的radd()方法。
增量赋值运算符:
__iadd__(self, other)定义赋值加法的行为:+=
__isub__(self, other)定义赋值减法的行为:-=
__imul__(self, other)定义赋值乘法的行为:*=
__itruediv__(self, other)定义赋值真除法的行为:/=
__ifloordiv__(self, other)定义赋值整数除法的行为://=
__imod__(self, other)定义赋值取模算法的行为:%=
__ipow__(self, other[, modulo])定义赋值幂运算的行为:**=
__ilshift__(self, other)定义赋值按位左移位的行为:<<=
__irshift__(self, other)定义赋值按位右移位的行为:>>=
__iand__(self, other)定义赋值按位与操作的行为:&=
__ixor__(self, other)定义赋值按位异或操作的行为:^=
__ior__(self, other)定义赋值按位或操作的行为:|=
一元运算符:
__neg__(self)定义正号的行为:+x
__pos__(self)定义负号的行为:-x
__abs__(self)定义当被abs()调用时的行为
__invert__(self)定义按位求反的行为:~x
属性访问:
__getattr__,__getattribute__,__setattr__和__delattr__
__getattr__(self, name): 定义当用户试图获取一个不存在的属性时的行为。
__getattribute__(self, name):定义当该类的属性被访问时的行为
(先调用该方法,查看是否存在该属性,若不存在,接着去调用__getattr__)。
__setattr__(self, name, value):定义当一个属性被设置时的行为。
__delattr__(self, name):定义当一个属性被删除时的行为。