面向对象进阶
1. 反射与自省
反射指的是通过 字符串来操作对象属性 ,涉及到四个内置函数的使用,getattr, hasattr,setattr, delattr,自省是在运行时能够获得对象的类型, type, isinstance, dir, 等
class Foo:
def __init__(self, name):
self.name = name
def f1(self):
print('from Foo.f1')
obj = Foo('action')
print(obj.__dict__)
# 结果: {'name': 'action'}
print(obj.name)
# 结果: action
"""
hasattr(obj,name_str): 判断objec是否有name_str这个方法或者属性
hasattr 判断obj是否有name这个方法或者属性
"""
print(hasattr(obj, 'name'))
# 结果: true
if hasattr(obj, 'f1'):
"""
getattr(obj,name_str): 获取object对象中与name_str同名的方法或者函数
getattr 获取object对象中与 f1 同名的方法或者函数
"""
f1 = getattr(obj, 'f1')
f1()
res = getattr(obj, 'xxx', None)
print(res)
# 结果: None
"""
setattr(obj,name_str,value): 为object对象设置一个以name_str为名的value方法或者属性
setattr 为object对象设置一个以age为名的18方法或者属性
"""
setattr(obj, 'age', 18)
print(obj.__dict__)
"""
delattr(obj,name_str): 删除object对象中的name_str方法或者属
"""
delattr(obj, 'age')
print(obj.__dict__)
# type 判断对象类型
a = 123
print(type(a))
# 结果:
# dir 带参数时获得该对象的所有属性和方法;不带参数时,返回当前范围内的变量、方法和定义的类型列表
print(dir(Foo))
# 结果: ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'f1']
# isinstance 判断对象是否是已知类型
res = isinstance(a, int)
print(res)
# 结果: True
2. 内置方法
python中,类机制内置很多的特殊方法来帮助使用者高度定制自己的类,这些内置方法都是以双下划线开头和结尾的,会满足某种条件时自动触发,常用的有__str__和__del__等
__str__() or __repr__()
当使用print输出对象的时候,默认打印对象的内存地址。如果类定义了__str__方法,那么就会打印从在这个方法中 return 的数据, __str__ 方法其实调用了 __repr__方法
class People:
def __init__(self, name, age):
self.name = name
self.age = age
def __str__(self):
return f'姓名:{self.name}, 年龄:{self.age}'
def __repr__(self):
return f'姓名:{self.name}, 年龄:{self.age}'
people = People('cross', 20)
print(people)
# 结果: 姓名:cross, 年龄:20
__del__()
当删除对象时,python解释器也会默认调用__del__()方法
class Washer():
def __init__(self, width, height):
self.width = width
self.height = height
def __del__(self):
print(f'{self}对象已经被删除')
haier1 = Washer(10, 20)
# <__main__.washer object at>对象已经被删除
del haier1
__new__()
__new__是 python类中的 构造方法,它先于__init__方法之前执行,python中可以用它来实现单例模式一个类只有一个实例
class People:
def __new__(cls, *args, **kwargs):
print("__new__", cls)
instance = object.__new__(cls)
return instance
def __init__(self, name, age):
print("__init__", self)
self._name = name
self._age = age
p = People("action", 25)
"""
__new__
__init__ <__main__.people object at>
"""
__new__ 和 __init__ 都被调用了。__new__ 方法用于创建对象并返回对象,当返回对象时会自动调用 __init__ 方法进行初始化,__new__ 方法是静态方法,而 __init__是实例方法
单例模式
class Person:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
p = Person()
p1 = Person()
print(id(p), id(p1))
# 结果: 4364569456 4364569456
print(p is p1)
# 结果: True