Python class methods analysis of __init__ and __del__ construction and destruction process

Source: Internet
Author: User
The recent study of the Python reference manual in the class section has encountered the problem of the class's Structural destruction section:

1. When are they constructed?
2. When is the destruction of the structure?
3. How are member variables handled?
4. How is the shared member function in Python accessed?
------------------------
Explore the process:
1, after the search, Python does not have a dedicated structure and destructors, but generally can be done in __init__ and __del__ respectively initialization and deletion operations, can be used to replace the construction and destruction. There is also a __new__ that is used to customize the creation of classes, but requires a certain configuration and is not discussed here.
2, the member function of the class is equal to public, but the default start is __ is a private variable, although it is private, but we can also access by some means, that is, Python does not have a real private variable. Such as:

The code is as follows:


__privalue = 0 # is automatically deformed as a member variable of "_ Class name __privalue"

3. Because of the particularity of Python, global member variables are shared, so instances of the class do not allocate content space specifically for it, similar to static, see the example below.

Test 1:

The code is as follows:


# Encoding:utf8

Class Newclass (object):
Num_count = 0 # All instances share this variable, i.e. each instance is not individually assigned
def __init__ (self,name):
Self.name = Name
Newclass.num_count + = 1
Print Name,newclass.num_count
def __del__ (self):
Newclass.num_count-= 1
Print "Del", Self.name,newclass.num_count
def test ():
Print "AA"

AA = Newclass ("Hello")
bb = Newclass ("World")
CC = Newclass ("AAAA")

Print "Over"

Debug Run:

The code is as follows:


Hello 1
World 2
AAAA 3
Over
Deexception L Hello 2
Attributeerror: "' Nonetype ' object has no attribute ' Num_count '" in <__main__.newclass object="" at="" 0x01af18d0="">> ignored
Exception attributeerror: "' nonetype ' object have no attribute ' Num_count '" in <__main__.newclass object="" at="" 0x01af1970="">> ignored


We found that Num_count is global, and when each instance is created, __init__ () is called, and the value of Num_count is increased by one, and when the program is finished, all instances are refactored, that is, __del__ () is called, but an exception is thrown at this point. This exception occurs when you view the exception as "Nonetype", which is when the Newclass has been garbage collected.

But is the question coming? Why is that? In accordance with the experience of C + + and other languages, should not be so ah! After looking for information, found:

Python's garbage collection process is not the same as in common languages, and Python is garbage collected in dictionary order, rather than in the order of creation. So when the system is recycling resources, it will be in the order of the class name A-za-z, in turn, we cannot control the process here.

To understand this, we try to do the following:

The code is as follows:


# Encoding:utf8

Class Newclass (object):
Num_count = 0 # All instances share this variable, i.e. each instance is not individually assigned
def __init__ (self,name):
Self.name = Name
Newclass.num_count + = 1
Print Name,newclass.num_count
def __del__ (self):
Newclass.num_count-= 1
Print "Del", Self.name,newclass.num_count
def test ():
Print "AA"

AA = Newclass ("Hello")
bb = Newclass ("World")
CC = Newclass ("AAAA")

Del AA
del bb
del cc

Print "Over"

Debug output:

The code is as follows:


Hello 1
World 2
AAAA 3
Del Hello 2
Del World 1
Del AAAA 0
Over

OK, everything happens in the order we expect.
However, we can not always manually recycle it? What's the point of doing Python's own garbage collection?

So, to continue looking, we can also access the class itself through self.__class__, and then access its own shared member variable, the Self.__class__.num_count, to replace Newclass.num_count in the class with self.__ Class__.num_count compile and run as follows:

The code is as follows:


# Encoding:utf8

Class Newclass (object):
Num_count = 0 # All instances share this variable, i.e. each instance is not individually assigned
def __init__ (self,name):
Self.name = Name
Self.__class__.num_count + = 1
Print Name,newclass.num_count
def __del__ (self):
Self.__class__.num_count-= 1
Print "Del", Self.name,self.__class__.num_count
def test ():
Print "AA"

AA = Newclass ("Hello")
bb = Newclass ("World")
CC = Newclass ("AAAA")

Print "Over"

Results:

The code is as follows:


Hello 1
World 2
AAAA 3
Over
Del Hello 2
Del World 1
Del AAAA 0

perfect! We have handled this problem perfectly!

Ps:

The book also mentions a number of questions to be supplemented here (for reference only):

__new__ () is the only method that executes before an instance is created, and is typically used when defining a meta-class.

del XXX will not actively invoke the __del__ method, only the reference count ==0, __del__ () will be executed, and the instance that defines __DEL_ () cannot be collected by Python's cyclic garbage collector, so try not to customize __del__ (). In general, __del__ () does not destroy the garbage processor.

The experiment found that garbage collection automatically called the __del__, which is inconsistent with what the book said, do not know what the reason, need to continue to study.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.