1. Briefly describe Python's garbage collection mechanism (garbage collection)
Garbage collection in Python is based on reference counting, supplemented by mark-clear and generational collection.
• Reference count: Python stores the reference count of each object in memory. If the count becomes 0, the object will disappear and the memory allocated to the object will be released. • Mark-clear: Some container objects, such as list, dict, tuple, instance, etc. may have reference cycles. For these cycles, the garbage collector will periodically recycle these cycles (the objects are connected by references (pointers) to form In a directed graph, objects form the nodes of this directed graph, and reference relationships form the edges of this directed graph). • Generational collection: Python divides the memory into three generations based on the survival time of the objects. After the objects are created, the garbage collector will allocate the generation they belong to. Each object is assigned a generation, and the younger generation is assigned priority, so objects created later are easier to be recycled.
1、__ new__There must be at least one parameter cls, which represents the current class. This parameter is automatically recognized by the Python interpreter when instantiated.
2、__ new__Must have a return value, return the instantiated instance, this is achieved by yourself__new__Pay special attention to when you can return the parent class (via super(Current class name, cls))__new__Out of the instance, or directly the object__new__Examples coming out.
3、__ init__There is a parameter self, which is this__new__Returned instance,__init__in__new__Some other initialization actions can be completed on the basis of__init__No return value is required.
4、 in case__new__Create an instance of the current class, it will be called automatically__init__Function, called by the return statement__new__函数的第一个参数是 cls 来保证是当前类实例,in case是其他类的类名,;那么实际创建返回的就是其他类的实例,其实就不会调用当前类的__init__Function, it will not call other types of__init__function.
1、 Variable data types: list, dict, set 2. Immutable data types: int/float, str, tuple Principle: Variable data types share a memory space address, and immutable data types mean that each object will generate one Memory address.
a,b=1,2
a,b=b,a
• Simple, direct, rude and effective is to use print() to print out the variables that may be problematic. •Anywhere print() is used to assist in viewing, you can use assert instead. • Replacing print() with logging is the third method. Compared with assert, logging will not throw an error and can be output to a file. • Start the python debugger pdb, let the program run in a single step mode, you can check the running status at any time.
dic ={"name":"zs","age":18}
dic.pop('name')
del dic['age']print(dic)
__ init__:Object initialization method
__ new__:The method executed when creating the object, the single-column mode will be used
__ str__:When using print output object, as long as you define it yourself__str__(self)Method, then it will print the data returned from this method
__ del__:Method of deleting object execution
• The difference between sort() and sorted() is that sort rearranges the list in place, while sorted() generates a new list. •Sorted(L) returns a sorted L without changing the original L. L.sort() operates on the original L. After the call, the original L will change without a return value; so a = a.sort( ) Is wrong! a = sorted(a) is right. •Sorted() is suitable for any iterable container, list.sort() only supports list (itself is a method of list), sorted is used more frequently than list.sort(), so more advanced sorting techniques in Python Demonstrate by sorted()
import json
dic ={"name":"zs"}
res = json.dumps(dic)print(res,type(res))
ret = json.loads(res)print(ret,type(ret))
is: Compare whether the memory addresses on both sides are equal, is to compare whether the two references point to the same object (reference comparison)
== : Compare whether the values on both sides are equal.
a =[1,2,3]
b = a
print(a is b)print(a == b)
c = copy.deepcopy(a)print(a is c)print(a == c)-------------
True
True
False
True
Recommended Posts