Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky
Concept of reference
A Reference is a pointer to an object
- A reference is a pointer to a real object in memory, represented as a variable name or memory address
- Every object has at least one reference,
id()
The function is used to get a reference - When passing arguments and assignments, Python passes references to objects rather than copying them
The sample code
Print (id(list2)) # 2044656837192 print(id(list2)) # 2044656837192 List2 refers to List1, both of which refer to the most basic object classes, so the result is the same for bothCopy the code
Handling of references by Python’s internal mechanisms
- Immutable objects:immutableThe interpreter maintains as few memory areas as possible for the same value
- Mutable objects:mutableThe interpreter maintains a different memory region for each object
Example Code 1
Text4 = text4 + text5 print(id(text1)) # 1616972638288 print(id(text2)) # 1616972638288 print(id(text3)) # 1616972638288 print(id(text4)) # 1616973621272 print(id(text5)) # 1616973578032 print(id(text6)) # 1616974246288Copy the code
Because Text1 and 2 are a referenced string, the memory address is the same; Because the Python interpreter can save a lot of memory, Python automatically references an address space when immutable types have the same values, so text1/2/3 address space is consistent. The Python interpreter does not optimize the address space for the computed result. Even if the two values are the same, the Python interpreter creates a new address space for the computed result
Example Code 2
list1 = []
list2 = []
list3 = []
print(id(list1)) # 3204114440776
print(id(list2)) # 3204114440840
print(id(list3)) # 3204115873544
Copy the code
Each mutable object has its own independent address space and does not reuse the address space
There are generally four ways to get a +1 reference
- Object created
- Object referenced
- An object is taken as an argument to a function or method
- Objects are treated as elements in a container
There are four cases that lead to reference -1
- Object deleted
- The name of the object is assigned to the new object
- Object out of scope
- The container in which the object resides is deleted
Object copy
Copy is to copy an object into a new object with “changed” memory space. The copy can be divided into shallow copy and deep copy
- Shallow copy: Copy only the top-level object. The default copy mode
- Deep copy: Copy all objects iteratively
Sample code (shallow copy 1)
List1 = [" ", [1, 2, Copy () # copy() # copy() # copy() # copy() # copy() # copy() # copy() # copy() list2, list3, list4]: for i in ch: Print (I, id(I), "\t", end="") print(ch, id(ch)) print(ch, id(ch)) [1, 2, 3] 2905787490888 [' one bowl week ', [1, 2, 3] 2905817180184 [' one bowl week ', [1, 2, 3] 2905787490888 [' one bowl week ', [1, 2, 3] [1, 2, 3] [' one bowl week ', [1, 2, 3]] 2905817180184 [1, 2, 3] 290581717180184 [1, 2, 3] 290581717180184 [1, 2, 3] [1, 2, 3] [1, 2, 3]Copy the code
A shallow copy is only the memory space of the copied list layer. The memory space of the elements in the list layer will not be copied
Sample code (shallow copy 2)
List1 = [" week ", [1, 2, Copy () # copy() # copy() # copy() # copy() # copy() # copy() # copy() # copy() # copy() # copy() # Print (list1) print (list2) print (list3) print (list4) "' - output - [' a bowl of week, [1, 2, 3, 4]] [' a bowl of week, [1, 2, 3, 4]] [' a bowl of week, [1, 2, 3, 4]] [' a bowl week ', [1, 2, 3, 4]]Copy the code
Only the list4 data is being modified, but all the list contents are being modified; This is because every list references the same thing, so changing one of four changes things
Deepcopy uses the deepcopy() method in the copy library, iterates through the inner layers of the copy object, completely opens up the memory space to create the object and all the object elements in the lower layer of the object. Deepcopy is only for mutable types, and cannot create new objects for immutable types
The sample code
[1, 2, 3]] list2 = copy. Deepcopy (list1) # copy for ch in [list1, list2]: for i in ch: Print (I, id(I), "\t", end="") print(ch, id(ch)) print(ch, id(ch)) [1, 2, 3] 2190853845832 [' one bowl week ', [1, 2, 3] 2190823984184 [1, 2, 3] 2190853961544 [' one bowl week ', [1, 2, 3]] "' 2190853961480Copy the code
Because the “sweet” string is an immutable type, its address space does not change; the rest of the address space does
Instance method reference
Instance methods are also a reference, a reference to the object itself, and when a method is referenced, the method (that is, the function) produces an object: the method object
Class property decorator
The @property decorator can change methods into visible “properties” that are represented as methods inside the class and properties outside
The sample code
Class TestClass: def __init__(self, name): self. Name = name @property Def age(self, value): if value < 0 or value > 110: Value = 19 self.__age = value tt = TestClass("一 个 周") bb = TestClass("一 个 周") tt.age = 18 bb.age = -19 print(tt.age) # 18 print(bb.age) # 19Copy the code
Class name modifier
Name Mangling is a convention for converting names in classes. Python uses Name Mangling to perform some important functions.
_name
name_
__name
__name__
_
_
Name modifier starting with a single underscore
- Attributes or methods that start with a single underscore are conventions used internally by classes and are specified in PEP8
- Just the agreement, still can pass
< object name >.< attribute name >
Access to - The difference in function is usage
from XX import *
Does not import properties or methods starting with a single underscore
The sample code
Class TestClass: def __init__(self, name): self._name = name print(tt._name) #Copy the code
Although the contract is used internally, it can still be accessed
_
Name modifier ending with a single underscore
Properties or methods ending with a single underscore should avoid conflicts with reserved words or existing names, as PEP8 prescripties. this is only a convention and does not have any corresponding functions
__
Name modifier beginning with a double underscore
Attributes or methods that start with a double underscore are renamed by the interpreter to avoid naming collisions. This is not a convention, but a functional one. __nama is changed to _< class name >__name to implement private attributes and methods. This is a kind of name modifier that is used indirectly as a private property or method
__name__
Double underscore name qualifiers at the beginning and end
Properties or methods that start and end with double underscores do not have any special functions. Their names cannot be changed. Part of their names are reserved properties or reserved methods
Single underline
Is the single underscore just a name that doesn’t matter? No special function
Python’s smallest empty class
Function:
-
A class is a namespace, and the smallest empty class can be used as a namespace
- Minimal empty classes can aid storage and use
- Dynamically adding attributes is a feature of Python classes
The sample code
Print (a.ext) # print(a.ext) # print(a.ext) # print(a.ext) # print(a.ext) #Copy the code