This is 22 days of my participation in the November Gwen Challenge. For more details, see: the last Gwen Challenge 2021.

Program main entry

Most of the time, we often see a statement like this in python programs:

if __name__ == '__main__'
Copy the code
  1. __name__Is a built-in property that all modules have.
  2. modular__name__The value depends on how you call the module.

If you have a test.py file and import the module import test.py in the a.py file using import, the __name__ attribute of the test.py module will be test, without path or file extension.

Sequential execution simply means that the code is executed from top to bottom.

Magic methods

In Python, there are special methods built in that are automatically called when certain operations are performed. These are called magic methods. Magic method names are always surrounded by double underscores:

  • The name of the __ __

Common magic methods

1. __doc__
  • __doc__To view the class documentation:
print(list().__doc__)
Copy the code
  • View the documentation for custom classes:
Class Demo(object): """ "pass d = Demo() print(d.__doc__)Copy the code
2. __module__

__module__ is used to view the module of the current operating class. When the execution module is the module of the class, the execution result is __main__. Otherwise, the result is the name of the module in which the class resides.

3. __class__

__class__ is used to view the current object’s class.

4. __dict__

__dict__ is used to get the attribute dictionary of a class or instance

⚠ ️ note:

  • Normal fields are stored in objects, so passObject __dict__.We get a normal field
  • Members other than normal fields are stored in the class, so they passClass __dict__.In order to get.
5. __del__()

The __del__() method is also called the destructor method. Execution is triggered automatically when an instance object created by this class is deleted or freed in memory.

⚠ ️ note:

  • Automatically triggered when all code has been executed__del__()
  • If you need to trigger in advance, you need to passdelKeyword, triggered after deleting all objects__del__()
  • This method generally does not need to be defined becausePythonIs a high-level language, programmers in the use of no need to care about the allocation and release of memory, are generally handedPythonThe interpreter performs. Therefore, the destructor call is automatically triggered by the interpreter during garbage collection.
6. __call__()

The __call__() method is used to turn an object into a callable object. That is, when a class has a __call__() method, the instantiated object is callable:

class Demo(object):
    pass

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

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

The __new__() method creates and returns an object. Called when the class is ready to instantiate itself:

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

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

d = Demo()
Copy the code

⚠ ️ note:

  • __new__()Method is used to create objects;
  • __init__()Method is called automatically when the object is created.
  • But I’m overwriting the superclass__new__()Method that overrides the parent class__new__()Object creation function, so the object was not created successfully. So just execute__new__()Method internal code.

Object creation execution order

  1. through__new__()Method to create an object;
  2. And return the object to pass__init__().
class Demo(object):
    def __init__(self):
        print("__init__")

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

d = Demo()
Copy the code

⚠ ️ note:

  • When creating an object, be sure to return it. It will be triggered automatically__init__()Methods;
  • __init__()methodologicalselfIn fact, it is__new__The instance returned, that is, the object;
  • __init__()with__new__()The difference between;
  • __init__Instance method,__new__Static method;
  • __init__Automatically called after the object is created,__new__Method to create an object.