__del__
Item series
__getitem__
__setitem__
__delitem__
__hash__
__eq__
Construction method Request a space
The destructor method frees a space before executing
An object borrowed the resources of the operating system, but also through the destruction method to return: File resource network resources
# garbage collection Mechanism class A: def __del__ (self): # the destructor of the Del a object automatically triggers this method Print (' execute me '= A ()del a # deletion of the Object del Print (a)
classFile ():#of the processing file def __init__(Self,file_path): Self.f=Open (file_path) self.name='Alex' defRead (self): Self.f.read (1024) def __del__(self):#is to return/release some of the resources borrowed while creating the object #del object when the programmer triggers #The Python interpreter's garbage collection mechanism is automatically triggered by Python when it reclaims the memory that this object occupies .Self.f.close () F= File ('file name') F.read ()
Whether active or passive, this f-object is always cleaned up, and the __del__ method is triggered when it is cleared, triggering this method to return the operating system's file resources
What the Python interpreter can do internally.
Apply for a space operating system assigned to you
Everything within this space is managed by your Python interpreter.
Objects--memory
f = open ('wenjian') # Python---operating system----file operators on hard drive f.close () # File Operators # del f #释放完后自动帮你关闭文件
Item series and objects using [] access values are linked
obj = {'k':'v'}print(obj) # object of the dictionary Print (obj['k'])
In the built-in module,
There are special methods that require that an object must implement __getitem__/__setitem__ to use
classB:def __init__(self,lst): Self.lst=LSTdef __getitem__(self, item):returnSelf.lst[item]def __setitem__(self, Key, value): Self.lst[key]=valuedef __delitem__(self, Key): Self.lst.pop (key) b= B (['111','222','CCC','DDD'])Print(b.lst[0])Print(b[0]) b[3] ='Alex'Print(B.LST)delB[2]Print(B.LST)
hash method
hash is an algorithm
to ensure that the hash result of different values is not the same
' 127647862861596 ' ==> 927189778748
But the hash value will never change when the same value executes the Python code at the same time
print (Hash (' abc ')) # 6048279107854451739
print (Hash (' abc '))
print (Hash (' ABC ')
dictionary addressing -hash algorithm
D = {'key':'value'}# Hash-built-in function
Set Set
se = {1,2,2,3,4,5, " a , " b , ' d ' , ' f ' ' print (SE)
D = {'key':'v1','key':' v2'}print(d['key')
Hash (obj) #obj内部必须实现了__hash__方法
__eq__
classA:def __init__(self,name,age): Self.name=name Self.age= Agedef __eq__(self, Other):ifSelf.name = = Other.name andSelf.age = =Other.age:returnTruea= A ('Alex', 83) AA= A ('Alex', 83) Aa2= A ('Alex', 83) Aa3= A ('Alex', 83) AA4= A ('Alex', 83) Aa5= A ('Alex', 83) Aa6= A ('Alex', 83)Print(A,AA)Print(Aa3 = = AA = = AA4)#= = This grammar is completely and __eq__
python-Object-oriented-built-in method complements