Two built-in functions:
There are many built-in functions, two are documented here.
- isinstance () Determine the type of object, including inheritance
- issubclass () to determine the inheritance relationship between classes
class A:
pass
class B(A):
pass
b = B()
print(isinstance(b,B))
print(isinstance(b,A))
#两个都返回True
class A:pass
class B(A):pass
print(issubclass(B,A)) #返回True,B是A的子类
print(issubclass(A,B)) #返回False
___first name__:
There are four
ways to call it: it is a special method in the class,
double down method,
magic method,
built-in method
_call_:
class A:
def __call__(self,*args,**kwargs):
print("执行call方法")
a = A()
a() #对象名() 相当于调用__call__方法
#或者
a = A()()
___len ___:
class Mylst:
def __init__(self):
self.lst = [1,2,3,4,5,6]
self.name = 'alex'
def __len__(self):
print("执行__len__了")
return len(self.lst)
l = Mylst()
print(len(l))
len (obj) is equivalent to calling the obj's __len__ method
_ len_ method return value is the return value of the len function
If an obj object does not have a __len__ method, then the len function will report an error
___new ___: Constructor
class Single:
def __new__(cls, *args, **kwargs):
obj = object.__new__(cls)
print("在new方法里",obj)
return obj
def __init__(self):
print("在init方法里",self)
s = Single()
1. Open a space that belongs to the object
2. Pass the space of the object to self, execute init
3. Return the space of this object to the caller