Other types of methods python

1. item series 
in the built-in module,
there are some special methods, the object must be achieved requires __getitem __ / __ setitem__ to use
2. __del__
configured to apply a method of spatial
destructor before releasing a space performing
a target operating system borrows resources, but also to return back through the destructor method: resource files, network resources,
whether active or passive, the object of this object will always be cleared away, it will be cleared away __del__ trigger method,
trigger this method will return operation file system resource
is a garbage collection mechanism

python interpreter able to get inside the thing.
apply for a space, the operating system assigns to you
all thing in this space of the, return your python interpreter to manage

a __hash__ 3.
hash method
underlying data structure based on the optimal operation of the hash address
hash algorithm is
able to a certain value in memory by the presence of a series of calculations to
hash result to ensure that different values are not the same
'2431231234355687757' ==> 234 728 368 923
in the run python on behalf of the same value multiple times When the hash value is different
However, when the same hash value to a value in the same execution code is constant python

collection set - hash algorithm to hash determination, then determination value
(first hash determination (fast), when the two hash values are different, and the memory address the same, and then determine the value (slowly, to one's judgment))
when the value of a hash, draw a memory address, but there there is data only to determine the value and I want to store the values are the same,
as if to cover heavy, if not the same, the second address to the value of another place to
hash (obj) # obj internal method must implement a __hash__

4. __eq__
"==" this syntax is completely and triggered __eq__


1. item series
class File:
    def __init__(self,lst):
        self.lst = lst
    def __getitem__(self, item):
        return self.lst[item]
    def __setitem__(self, key, value):
        self.lst[key] = value
    def __delitem__(self, key):
        self.lst.pop(key)

F = File ([ " Wang " , " Bob " , " Sally " , " Li " , " Huang " ])
 Print (f.lst [0]) # the __init__ method 
Print (F [0])      # the __getitem__ 
F [2] = " all "      # the __setitem__ modified 
Print (f.lst [2 ])
 Print (f.lst)
 del F [. 4]         # __delitem__ is 
Print (f.lst)

 

2. __del__ ==> destructor

class File:
     # handling files, first create a file_path 
    DEF  __init__ (Self, file_path):
        self.f = Open (file_path)     # f is the object, self.f a file handle

    def read(self):
        RET = self.f.read (. 5)     # self.f a file handle, to read the file contents inside 
        Print (RET)   # Content

    DEF  __del__ (Self):   # is to return / release some resources in the creation of some objects borrowed

        # Del objects when it is triggered by the programmer 
        # Python interpreter garbage collection, recycling memory occupied by the object when, python automatically triggered 
        Print (555 )
        self.f.close ()   # close the file 

F = File ( ' file_path ' ) # automatically triggers this method __del__ 
Print (F)
 # delete del del f # object trigger configuration analysis method 
# print (f) # will error 
f.read ()     # execute print function

 

3. __hash__ method

# The hash method 
Print (the hash ( " the hash " ))
 Print (the hash ( " the hash " ))
 Print (the hash ( " the hash " ))
 # Code plurality of rows with the same value are performed once for the same, 
# repeatedly performed value is not the same (that is, the values and the second performance of the first performance is not the same)

# Hash - built-in functions 
    # SET set - hash algorithm to hash determination, then determination value 
    # (first hash determination (fast), when the two hash values differ, but the same memory address, then determination value (slowly, to a a judgment)) 
    # when the value of a hash, draw a memory address, and there exist data only when this value judgment and I want to store values are all the same, 
    # if the same coverage to heavy, if not the same secondary addressing this value to another place

    # Hash (obj) obj # interior must implement a method __hash__

# Set - hash algorithm 
# info = { "DS", #} 2,4,3,43,4,1's in the set has a hash algorithm 
# Print (info) 
# Dictionary - hash algorithm 
# DIC = { "Key" : "value"} # dictionary to find value fast, with hash algorithm

 

Method 4. __eq__

class file:
    def __init__(self,name,age):
        self.name = name
        self.age = Age
     DEF  __eq__ (Self, OTHER):
         IF the self.name == other.name and self.age == other.age:    # determines if the value returned is equal to True 
            return True
         the else :
             return False

a = file("小明",18)
A1 = File ( " Bob " , 18 )

Print (A, A1)
 Print (A == A1)    # same value of the object, True 
Print (A IS A1)   # determines different memory address, False

# Above is similar to 
B = { ' Bob ' : 18 }
B1 = { ' Bob ' : 18 is }
 Print (ID (B), ID (B1))
 Print (B == B1)        # "==" Here the values are equal, True 
Print (B IS B1)       # same value, but the memory not the same address

 

 

 

 

 

 

 

 

 






Guess you like

Origin www.cnblogs.com/Pengdachui-1/p/11980505.html