“A quitter never wins and a winner never quits” — Napoleon Hill
Garbage collection is the process of finding data objects that will not be accessible in the future in a running program, and reclaiming the resources (especially memory) used by those objects. Languages for automatic garbage collection--Java, C#, Python and most scripting languages. C is a language without garbage collection-programmers need to know when to allocate and reclaim memory.
Ax = B
and By = A
, so the reference count will not change to below 1, even if there are no other objects Reference A and B, in this case, the garbage collector will periodically look for and delete them.Refer to the previous article: [Learning Python for a year, this time I finally understand shallow copy and deep copy] (https://links.jianshu.com/go?to=https%3A%2F%2Fyuzhoustayhungry.github.io%2Fpost%2F%25E6%25B7%25B1%25E6%258B%25B7%25E8%25B4%259D%25E5%2592%258C%25E6%25B5%2585%25E6%258B%25B7%25E8%25B4%259D%2F)
Q: What is the difference between shallow copy and deep copy?
answer:
copy.copy(x)
and copy.deepcopy(x)
, shallow copy will construct a new composite object, and then (to the extent possible) insert references to the objects found in the original object. Deep copy will construct a new compound object, and then recursively insert a copy of the object in the original object.Q: What is the difference between an iterator and a generator?
**Iterator: ** An object that implements the __iter__()
and __next__()
methods. The first method returns the iterator object itself and is used in for and in statements. The first method returns the next value in the iteration. If there are no more elements, a StopIteration
exception will be raised.
**Generator: ** An easy way to create iterators, use the keyword yield
. Generators use the function call stack to implicitly store the state of the iterator-compared to writing the same iterator as an explicit class, it simplifies the writing of iterators. It also helps improve readability.
Every generator is an iterator, but the reverse is incorrect. In particular, iterators can be fully mature classes and can therefore provide other functions. For example, it is easy to add a method to the iterator class above to change the iteration limit, which is impossible for generators.
@
Symbols are syntactic sugar for decorators. They are used when defining functions to avoid assignment operations again.
import time
def time_function(f):
def wrapper(*args,**kwargs):
begin = time.time()
result =f(*args,**kwargs)
end = time.time()print("Function call with argument {all_args} took ".format(
all_args="\t".join((str(args),str(kwargs))))+str(end - begin)+" seconds to execute.")return result
return wrapper
Lists[1, 4, 7, "apple", 4]
, Tuple(3.14, "PI", 2,43, "e")
**The same point: ** are containers, and they can store different types of data, and can be indexed for access a[i]
**Difference: ** The tuple is immutable, you cannot change the value of the index a[i]
, nor can you add/remove elements from the tuple; but the list can.
Benefits of immutability: performance improvement, container friendliness, thread safety. The ancestor can be placed in the set set
and used as a key value, but a list cannot. The creation and access speed of tuples is slightly faster, and the memory footprint is small.
Both are used to pass variable parameters in functions. *arg
is used to pass variable-length parameter lists:
args
-it can also be called A
or varargs
, args
is an idiom;*
Must be followed by regular parametersThe second parameter **kwargs
is used when passing a variable number of keyword arguments to the function.
args
, you can also call them D or argdict.**
Parameters must appear after all regular named parameters and *
parametersincrement_by_i =[lambda x: x + i for i inrange(10)]print(increment_by_i[3](4))
The program will print 13 (= 9 + 4) instead of the expected 7 (= 3 + 4). This is because the functions created in the loop have the same scope. They use the same variable name, so they all refer to the same variable i, which is 10 at the end of the loop, so it is 13 (= 9 + 4).
There are many ways to get the desired behavior. The reasonable way is to return the lambda from the function to avoid naming conflicts.
What are the positional parameters, keyword parameters and default parameters of a function.
Recommended Posts