Python常用魔法方法
1 、__new__()
__new__是在实例创建之前被调用的,用于创建实例,然后返回该实例对象
2、__init__()
__init__是当实例对象创建完成后被调用的,用于初始化一个类实例
class MyClass(object):
def __init__(self, name, age):
print('__init__ called')
self.name = name
self.age = age
def __new__(cls, *args, **kwargs):
print('__new__ called')
return super(MyClass, cls).__new__(cls)
# 执行
obj = MyClass('cc', 24)
print(obj.name)
print(obj.age)
# 输出
__new__ called
__init__ called
cc
24
3 、__str__()
对象被执行打印操作的时候自动触发, 该方法必须返回字符串类型的数据,很多时候用来更加精准的描述对象
class Myclass(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return '打印的变成我了'
# 执行
obj = Myclass('cc', 24)
print(obj)
# 输出
打印的变成我了
4、__call__()
对象被加括号调用的时候自动触发
class Myclass(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __call__(self, *args, **kwargs):
print('__call__ called', args, kwargs)
return '嘿嘿嘿'
# 执行
obj = Myclass('cc', 24)
obj(1, 2, 3, sex='male')
print(obj(1, 2))
# 输出
__call__ called (1, 2, 3) {'sex': 'male'}
__call__ called (1, 2) {}
嘿嘿嘿
5、__del__()
对象被执行(被动、主动)删除操作之后自动执行
class Myclass(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __del__(self):
print('__del__ called')
# 主动删除:使用del关键字主动删除
obj = Myclass('cc', 24)
del obj # 主动删除对象
print(obj.name)
# 输出
NameError: name 'obj' is not defined
__del__ called
------------------------------------
# 被动删除:即对象调用完后自动删除
obj = Myclass('cc', 24)
print(obj.name)
print(obj.age)
# 输出
cc
24
__del__ called
6、__getattr__()
对象查找不存在名字的时候自动触发
class MyClass(object):
def __init__(self, name, age):
print('__init__ called')
self.name = name
self.age = age
def __getattr__(self, item):
print('__getattr__ called')
return '找不到'
# 执行
obj = MyClass('cc', 24)
print(obj.name)
print(obj.age)
print(obj.sex)
# 输出
__init__ called
cc
24
__getattr__ called
找不到
7、__setattr__()
对象在执行添加属性操作的时候自动触发>>>obj.变量名=变量值
class Myclass(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __setattr__(self, key, value):
print('__setattr__ called')
print(key, value)
# 执行
obj = Myclass('cc', 24)
obj.sex = 'male'
# 输出
__setattr__ called
name cc
__setattr__ called
age 24
__setattr__ called
sex male
8、__getattribute__()
对象查找名字无论名字是否存在都会执行该方法
如果类中有'__getattribute__'方法 那么就'不会'去执行'__getattr__'方法
class Myclass(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __getattribute__(self, item):
print('__getattribute__ called', item)
def __getattr__(self, item):
print('__getattr__ called', item)
return '找不到'
# 执行
obj = Myclass('cc', 24)
print(obj.name)
print(obj.sex)
# 输出
__getattribute__ called name
None
__getattribute__ called sex
None
9、__enter__() 与 __exit__()
__enter__: 对象被执行with上下文管理语法自动触发
__exit__: 对象被执行with上下文管理语法结束之后自动触发
class Myclass(object):
def __init__(self, name, age):
self.name = name
self.age = age
def __enter__(self):
print('__enter__ called')
def __exit__(self, exc_type, exc_val, exc_tb):
print('__exit__ called')
# 执行
obj = Myclass('cc', 24)
with obj as f:
print('执行中...')
print(123)
# 输出
__enter__ called
执行中...
__exit__ called
123
标签:__,常用,obj,name,Python,self,魔法,print,age From: https://www.cnblogs.com/DTCLOUD/p/17357987.html作者:陈超