current position:Home>The second bullet of class in Python

The second bullet of class in Python

2022-01-31 19:57:44 A program cat

「 This is my participation 11 The fourth of the yuegengwen challenge 16 God , Check out the activity details :2021 One last more challenge

Methods in class

A method is a special function encapsulated in a class .

example :

class Person:
        def sing(self):           # Method , Writing functions in a class is called defining methods in a class .
                print(" I am the most handsome !")      # There is a default value none.   print(" I am the most handsome !",none)

wuhan = Person()               # Define examples , Required for writing .

wuhan.sing()        # Call the methods in the class .       Format : Instantiate objects . Method ()
Person.sing(self = ' Something to pass in ')
 Copy code 

Example method Call procedure and self:::
(1)self: Usually , Name the parameter that will be passed in by default as self, Used to represent the instance object that calls this method itself .
(2) Example method : Methods are always defined in classes , But it's called “ Example method ”, Because it represents the behavior common to all instances of the class .

Little knowledge points :::self It's an example , Who will use it , So who is it .
example :

class Person:
                def sing(self):
                        print(" The teacher is singing %s"%self.geci)
                        print(id(self))     # You'll find self Of id And the following laoshi Of id It's the same 
laoshi = Person()               # In this case ,laoshi Used self, therefore self Namely laoshi
print(id(laoshi))
# Just like the parameters of a function , Just pass an instance of itself first self

laoshi.geci = " Warm loneliness "

laoshi.sing()
 Copy code 

init Double underline

Initialization method , The function is to instantiate , Automatically call .( Magic methods )

“ initialization ” Special methods :
stay Python There are many fixing methods that start with a double underline and end with a double underline , They will be triggered to execute at a specific time .
__init__ Is one of them , It will be called automatically after instantiation . To complete the initialization of the instance .

When you create an object , Implement some initialization operations for objects , Provide some default values , Only when the instance attribute needs to be defined for the class will __init__ Method .

class Person:
        def __init__(self.name):
                self.name = name           #self.name Is the instance property ;name Is the method .

        def sing(self):
                print(' Wu is the most handsome ')



wumou = Person(" Wu ")   #  Instantiation        Automatically call our initialization method , Gave the name Wu to name
( This code means    wumou.name = " Wu "( Define an instance property )    wumou.name amount to self.name," Wu " It's like giving name)
 Copy code 

Magic methods : Every magic method has its specific function .

Little knowledge points :
stay python in , The point has been deleted , It will be recycled .
del Delete pointing to , If a variable points to no, it will be recycled .
del That's what's called __init__ Magic methods .
for example : In the list method ,del List name , When you check this list again, you will report an error , because del Delete the point .

del destructor , Also called destroy initialization method ( Destroy the one above ), After this file runs, it triggers .( Magic methods )

__del__ Is a destructor , When using del When deleting objects , Will call its own destructor . Prompt developers , The object is destroyed , Convenient debugging , Do some necessary cleaning work
Understanding is simple :( When the number of instance object references is 0 This magic method will be called when )
That is, when... Is used in the class del When magic methods , If used del Delete , Then it will call __del__ Magic methods ; If not used del Delete , So at the end of the program , Will automatically reclaim the memory address , Will call __del__ Magic methods .

introduction :

class Person:
        pass

hansha = Person()
print(hansha)         # Output is :hansha The instantiated object points to the object . That is to say :<__main__.Person object at 0x00000175C3AFC1D0>
del hansha 
print(hansha)         # The output will report an error , Because the point was deleted . That is to say :NameError: name 'hansha' is not defined
 Copy code 

A little upgrade :
python The execution of a program in , Memory space will be reclaimed , If you recycle it, there will be no point .

class Person:
        def __del__(self):        # Equivalent to rewriting python Of __del__ Method . Use... In the list del When deleting, you call python default __del__ Method .
                print(' study hard ')

hansha = Person()
print(11111)
print(22222)
 Output is :               
11111
22222
 study hard 
 Copy code 

Take a comparative example and take a closer look :
When the pointer is deleted, it will call __del__ Magic methods .

class Person:
        def __del__(self):
                print(' study hard ')

hansha = Person()
print(11111)
del hansha
print(22222)
 Output is :               
11111
 study hard 
22222
 Copy code 

Advanced version of summary :
Object destruction mechanism based on variable counting
When no variable points to an object ,python Will automatically destroy this object , To reclaim memory space .
del keyword , You can delete the point of a variable .

class Person:   

        def __init__(self,name):    # Initialization method 
                self.name = name

        def __del__(self): 			# destructor ( Destruction method )
                print(self.name," Destroyed ")

wuhan = person(" unknown ")
del wuhan      # Output is : Destroyed 
 Copy code 

copyright notice
author[A program cat],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201311957407969.html

Random recommended