Piecemeal accumulation III
The default value of a function's parameter is determined and saved in memory when the function is defined, and the function is called without a new space in memory and then re-assigned with the default value of the parameter, rather simply referencing the original address of the parameter. This brings up a pit, that is, when you make some changes to the default parameter in the function body, if the parameter default value is INT,STR this immutable type, then fortunately, because the default parameter does not change in memory, but the parameter points to another space; When you make changes to the default parameters, it will directly affect the actual value of the parameter in memory, which will affect the default value of the parameter the next time the function is called. Like what:
def foo (para=1): + = 1 print para>>>foo ()2>> >foo () # Second call result unchanged 2def foo (para=[1]) para.append (1) Print para>>>foo () [>>>foo]() # The second call shows some of the effects of the first call, because the default parameter value is a VARIANT data structure, and changes to it will actually be reflected in memory [1,1,1]
Gets the absolute path of the current script
Os.path.abspath (__file__)
__file__ this magical variable means to refer to the file itself
Determine if a class has a property (variable or method) with the Hasattr function. This function has two parameters, the first one is an instance of a class, and the second is a string that indicates the name of the property you want to check. Like what:
make s = "Hello". S is a string. Then hasattr (S, "strip") is to judge that the instance of S has no attribute named strip. In fact the string has strip this method, so return True
Third-party module CharSet can detect the encoding format of the string, install with Pip install CharSet
Use Charset.detect ("a string")
It is important to note that typing a Chinese string directly seems to be just the default encoding format for the file, because it is processed by Python in the default encoding format when it is stored in memory.
To see the source code for a module, you need to know the path to the module. After you import the module in Python, you can type the module name directly in the Pythonshell and the path will be displayed.
There are many ways to add members to the list class, such as:
[1].append (2)///The first two methods are directly modifying the contents of the in-memory list itself.
[1].extend ([2])
[1]+[2]//This method is actually to create a new list object in memory to put together two old list contents
From the effect of running speed, the third is the slowest because to create a new object
About the shape of variable & immutable object data in memory
In fact, this content should be learning Python at the earliest should understand, I did not care about the time to learn too much, and now also go back to look carefully again.
In Python, everything is the object. This means that a variable or function is simply referencing some real data that exists in memory and not being stored directly in memory. This begs the question, when I make some changes to the value of a variable, do I let the variable change to a Reference object, or do I modify it directly on the current reference object? Also, when I add a variable, the value is the same as the current variable, does it refer to the same piece of memory space, or does it refer to another piece of memory that is just the content? This difference is the difference between immutable and mutable types.
For int,str,tuple this immutable type. Variables of the same value refer to the same object, and changing the value of the variable is equivalent to changing the reference of the variable to point to another object:
>>>x = 1>>>y = 1>>>id (x) = = ID (y) True>>>x + = 1>>>id (x) = = ID (y ) False
For list,dict this mutable type. A variable of the same value refers to a different object as well. Changing the value of a variable is equivalent to changing it directly on an object in memory, but the reference relationship of the variable is constant:
>>>x=[1]>>>y=[1]>>>id (x) = = ID (y) False>>>oldxid = ID (x)>>>x.append (2)>>>id (x) = = Oldxidtrue
Overall, the benefit of immutable types is that multiple equivalents can reference only one object, saving memory. However, it is not good to create a variety of objects if the variable values change more.
By the way, is and = = are also the difference when judging the equivalence. is to determine whether the reference object is consistent. and = = Determines whether the value of the reference object is consistent.
"Python" fragmented knowledge accumulation III