python面向对象的内置函数和反射
内置函数
class A:
@property #property装饰器
def func(self):pass
class A:
def __init__(self,name):
self.__name = name
@property
def name(self):
return self.__name
@name.setter #负责对property方法进行修改
def name(self,new):
self.__name = new
class A:
def __init__(self,name):
self.__name = name
@property
def name(self):
return self.__name
@name.setter
def name(self,new):
self.__name = new
@name.deleter #让property方法具备删除的操作
def name(self):
del self.__name
class A: pass class B(A): pass a = A() b = B() print(type(a) is A) #True print(type(b) is A) #Flase print(isinstance(a,A)) #True print(isinstance(b,A)) #True,b是子类实例化的对象,所以和A也有继承关系 print(isinstance(a,B)) #Flase
class A: pass class B(A): pass print(issubclass(A,B)) #False print(issubclass(B,A)) #True,B是A的子类
- getattr
class Foo: def __str__(self): return 'Foo str' def __repr__(self): return 'Foo repr' class Son(Foo): def __str__(self): return 'Son str' def __repr__(self): return 'Son repr' son = Son() print(son)