current position:Home>How IOS developers learn Python Programming 22 - Supplement 1

How IOS developers learn Python Programming 22 - Supplement 1

2022-02-01 12:51:57 Sissy's father

This is my participation 11 Yue Gengwen challenges 22 God , Check out the activity details :2021 The last time Gengwen challenge .

The main entry of the program

A lot of times , We often python See such a line of statement in the program :

if __name__ == '__main__'
 Copy code 
  1. __name__ It is a built-in property that all modules have .
  2. A modular __name__ The value depends on how you call the module .

If you have one test.py file , If in a.py Use... In the document import Import this module import test.py, that test.py Modular __name__ The value of the property is test, No path or file extension .

In fact, sequential execution , Simply put, the code is executed from top to bottom .

Magic methods

stay Python in , There are some built-in specific methods , These methods are automatically called for specific operations , be called Magic methods .
Magic methods Names are always surrounded by double underscores :

  • __ name __

Common magic methods

1. __doc__
  • __doc__ Used to view the class's documentation : 
print(list().__doc__)
 Copy code 
  • View the description document of the custom class : 
class Demo(object):
    """
     I am a comment 
    """
    pass

d = Demo()
print(d.__doc__)
 Copy code 
2. __module__

__module__ Used to view the module of the class of the current operation . When the execution module is the module of the class , The execution result is __main__. otherwise , The execution result is the name of the module where the class is located .

3. __class__

__class__ The class used to view the current object .

4. __dict__

__dict__ Used to get the attribute dictionary of a class or instance

️ Be careful

  • Common fields are stored in objects , So pass object .__dict__ What you get is a normal field
  • Members other than ordinary fields are stored in classes , So pass class .__dict__ To get .
5. __del__()

__del__() This method is also called destruct method . When the instance object created by this class , Be deleted or released in memory , Will automatically trigger execution .

️ Be careful

  • When all the code is executed, it will automatically trigger __del__()
  • If you need to trigger in advance , You have to go through del keyword , Triggered when all objects are deleted __del__()
  • This method generally does not need to define , because Python It's a high-level language , Programmers do not need to care about the allocation and release of memory , It's usually given to Python The interpreter executes . therefore , The call of destructor is automatically triggered by the interpreter during garbage collection .
6. __call__()

__call__() Method is used to turn an object into a callable object . in other words , When a class has __call__() When the method is used , The instantiated object is callable (callable):

class Demo(object):
    pass

d = Demo()
d()       # TypeError: 'Demo' object is not callable
 Copy code 
class Demo(object):
    pass

d = Demo()
d()       # TypeError: 'Demo' object is not callable
 Copy code 
7. __new__()

__new__() Method is used to create and return an object . When the class is instantiated, it will call itself :

class Demo(object):
    def __init__(self):
        print("__init__")

    def __new__(cls, *args, **kwargs):
        print("__new__")

d = Demo()
 Copy code 

️ Be careful

  • __new__() Method to create an object ;
  • __init__() Method when the object is created , Automatically call ;
  • But the parent class is overridden here __new__() Method , It covers the parent class __new__() The function of creating objects , So the object was not created successfully . So only execute __new__() Method internal code .

Object creation execution order

  1. adopt __new__() Method to create an object ;
  2. And return the object to , Pass to __init__().
class Demo(object):
    def __init__(self):
        print("__init__")

    def __new__(cls, *args, **kwargs):
        print("__new__")
        return super().__new__(cls)

d = Demo()
 Copy code 

️ Be careful

  • When you create an object , Be sure to return the object to , It will automatically trigger __init__() Method ;
  • __init__() Among the methods self, It's actually __new__ The returned instance , That is, the object ;
  • __init__() And __new__() difference ;
  • __init__ Example method ,__new__ Static methods ;
  • __init__ Automatically call... After the object is created ,__new__ Method to create an object .

copyright notice
author[Sissy's father],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/02/202202011251548667.html

Random recommended