July 22, 2021 Destructor__ del___ Methods and__ cal__ Methods and methods are not overloaded

catalogue

1, Deconstruction method__ del __

II__ call__ Methods and callable objects

III Method no overload

IV Dynamic nature of the method:

V Private properties and private methods (implementation encapsulation)

Summary: everything is an object

1, Deconstruction method__ del __

The del method is used to implement the operations required for object destruction, such as releasing the resources occupied by the object, opening the network connection, etc,

Python implements an automatic garbage collection mechanism. When the object is not referenced (reference count is), it is called by the garbage collector__ del __ () method, which will be provided automatically__ del__ Method. Generally, there is no need to customize the destructor method. We can also delete the object through the Del statement. This method ensures that the method is called

II__ call__ Methods and callable objects

Defined__ call__ The object of () method becomes a callable object, that is, the object can be called like a function

III Method no overload

Multiple methods with duplicate names can be defined in other languages as long as the method signatures are consistent (the method signature includes: method name, parameter quantity and parameter type)

In Python, there is no type declaration for parameters, and the number of parameters is also determined (which can be controlled by variable parameters). Therefore, python does not have method overloading, but defining a method can have multiple call modes, which is equivalent to realizing method overloading

If multiple methods with the same name are defined, only the last one is valid (duplicate names are not recommended)

IV Dynamic nature of the method:

We can add new methods to the class or dynamically modify the existing methods of the class

class person:
	def work(self):
		print("Ah, this is a way")

		
def play_game(self):
        print("I love fishing")

	
def work2(self):
        print("Ah, that's the way to fish")


#————————————————————————————- results————————————————————————————————————————————

>>> 
#========================== RESTART: D:/Pwork/ff.py ==========================
>>> p=person()
>>> p.work()#Methods within a class can be called
 Ah, this is a way
>>> p.play_game()#Non class methods cannot be called
Traceback (most recent call last):
  File "<pyshell#36>", line 1, in <module>
    p.play_game()
AttributeError: 'person' object has no attribute 'play_game'
>>> p.work2()#Non class methods cannot be called
Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    p.work2()
AttributeError: 'person' object has no attribute 'work2'
>>> p.play=play_game#Play_ Add game to class
>>> p.play(1)#It can be run. If there are no parameters, an error will be reported. An example is as follows:
I love fishing


#---------------------------------------—
>>> p.work=work2
>>> p.work()#No parameter error
Traceback (most recent call last):
  File "<pyshell#45>", line 1, in <module>
    p.work()
TypeError: work2() missing 1 required positional argument: 'self'
>>> p.work(1)#The new method replaces work
 Ah, that's the way to fish
>>> 

V Private properties and private methods (implementation encapsulation)

Python has no strict access restrictions on class members. Private properties and private methods have the following points:

  • The Convention starts with a double underscore. The property is private and others are public
  • Private methods (properties) can be accessed directly inside the class
  • Private methods (properties) cannot be accessed directly outside the class
  • Outside the class, private properties (Methods) can be accessed through "(single underline) class name _ (double underline) private property (method) name"

Methods are essentially attributes! It's just that it can be executed through (). Therefore, the private attributes and public attributes mentioned here
It also explains the usage of private methods and public methods

example

class person:
    __name="No name"#The private class attribute dir can be found
    ame="name"
    def __init__(self,age,fish):
            self.__age=age#Class private instance properties
            self.fish=fish
    def work(self):#Common instance method
            
            print(f"Ah, this is a way,it{self.__age}Years old, it's called",person.__name)#Private properties can be accessed directly inside the class
    def __play(self):#Private instance method
            print(str(self.__age)+"Touch my old love",self.fish,"But I can't touch it")
	


>>> 
#========================== RESTART: D:/Pwork/ff.py ==========================
>>> p=person(12,"I'm not a fish")
>>> p.work()#Private properties can be accessed directly inside the class
 Ah, this is a way,It's 12 years old. It doesn't have a name
>>> p.ame#Public properties can be accessed directly
'name'
>>> p.__name#Private properties cannot be accessed directly
Traceback (most recent call last):
  File "<pyshell#73>", line 1, in <module>
    p.__name
AttributeError: 'person' object has no attribute '__name'
>>> 
#——————————————————————————External calls to private methods -——————————————————————————————————————————
>>> p._person__play()#Private methods cannot be accessed directly
12 I love to touch. I'm not a fish, but I can't touch it
>>> p.__play()#Private methods cannot be accessed directly
Traceback (most recent call last):
  File "<pyshell#128>", line 1, in <module>
    p.__play()
AttributeError: 'person' object has no attribute '__play'
>>> 


  

Summary: everything is an object

Note link: Method summary of python calling private properties - cloud + community - Tencent cloud (tencent.com)

                Use of private properties and private methods of python classes_ Chuji blog - CSDN blog_ python class private method

Added by afern03 on Sat, 15 Jan 2022 05:19:20 +0200