Encountered python about Del small problem, but also caused some problems, on the simple record, if there are different views, welcome to discuss together ~
Python is like Java is a garbage collection mechanism language, so we do not need to like C + + through destructors to manually free memory,
But Python also provides the __del__ release method. When an object's reference count is 0 o'clock, it is called automatically, so let's say
Reference Count Bar.
1. Reference count
#!/usr/bin/env python
#coding: UTF-8
import sys
class MyClass (object):
def __init__ (self):
print (' I am __init__ function ')
def __del__ (self):
print (' I am __del__ function ')
if __name__ = = ' __main__ ':
m1 = MyClass () m2
= m1< C9/>print (Sys.getrefcount (M1))
The results are shown below:
The result is surprisingly, there are only two instances, why the reference count is 3?
We all know that Python is a scripting language, an interpretive language, but I don't know if anyone notices that Python is actually compiling.
The PYC document is a good proof. can go to/usr/lib/python to see
So it's a little bit more subtle. Python is a language that is first compiled and interpreted, which refers to compiling the bytecode and then interpreting the bytecode one line at a time.
Usually the interpreter has done it for us, you might say that the above code can't see. pyc file, you put the function into a. py file
The import is then compiled and then you can see it. In fact, the. pyc file is only the performance of the compiled Pycodeobject stored on the hard disk.
The real result of the compilation is Pycodeobject
Process. PY-> Compilation-> Pycodeobject-> Interpretation (virtual machine execution)
Now that the reference count, a corner, actually does not count, because the reference count is not the correct value because of Python's compilation process.
Please refer to http://blog.csdn.net/balabalamerobert/article/details/1649490
2.__del__
When Python's reference count is 0, it automatically reclaims objects, so it's generally not recommended that we use the __del__ deletion method in Python.
I encountered a problem with my own use of __del__, or read the code:
Two classes defined, a base class MyClass have names and telephone information, derived classes Derclass add addresses, each class has an attribute property
GLB and DER_GLB.
#!/usr/bin/env python
#coding: UTF-8
class MyClass (object):
GLB = #global variable
def __init __ (self, NM, ph):
self.name = nm
Self.phone = ph
class Derclass (MyClass):
der_glb = #global Variable
def __init__ (self, NM, ph, addr):
super.__init__ (nm, ph) #python3
self.address = addr
def __del__ (self):
#del self.__class__.der_glb
del self.der_glb
print ' del der_glb '
if __ name__ = = ' __main__ ':
m1 = MyClass (' WWH ', ' 123 ')
m2 = derclass (' WWH ', ' 456 ', ' Xian ')
The main look down del function, I in the Del function, I called del self.der_glb to delete this all class common variables
(All instances of variables defined within a class in Python are shared)
The result is an error.
But I changed del Self.__class__.der_glb, but I was right.
What's the reason?
Previously, all instances of variables defined within a class in Python were shared, so each instance would be called when its own reference count of 0 was completed.
In other words, each instance is del again. Only one copy of the DER_GLB variable, of course, is wrong.
So why add __class__ right?
The Python memory model should be that after we have defined a class, the template module of that class will also store a copy (ID (der_class) in memory, after all, it has all the variables that the instance needs to use, and so on.
So the __class__ meaning should be to get the address of this module in memory, and then get the der_glb of module and delete it.
We certainly can't use it after we delete it.
Like Python delattr (obj, attr) Delete the properties of a class
After I have changed according to the above, Del Self.__class__.der_glb, run successfully, and once again define the instance of the class to access the Der_glb property error, the error results are as follows:
Visible after Del der_glb, again using DER_GLB display has no attibute ' der_glb ', proof that what I said earlier is correct, this attribute has been deleted
Explain that der_glb in Python is similar to a static variable in C + +, all instances are shared, but in Python they are called attribute attributes of the class.
While Python works well, it also needs to figure out some principles ^_^