I Memory allocation in python
1.
In python, variables are references to objects. Variables are on the stack and objects are on the heap.
Unlike java, the integer types 1, 2 and 3 in python are also stored in the heap as objects
a=1 # The left side of the equal sign is called a variable, and the right side of the equal sign is called an object. In python, variables have no type, and only objects have types.
In java, this can be understood as assigning 1 to variable a. But it can't be described like this in python. The correct thing is: reference a points to object 1.
2. Variable type and immutable type
There are two types of objects in Python, variable objects and immutable objects
Variable type: the data stored in the memory address of the object is variable. The variable objects are: list and dict
Immutable type: the data stored in the memory address of the object cannot be changed. Immutable objects include int, string, float and tuple
def fun(b): b+=1; a=1 print(a) #1 fun(a) print(a) #1
The above test looks like value passing in java because the value of argument a is not changed. But the reason is that when b+=1 is executed, instead of adding one to the object pointed to by b, the variable b is pointed to object 2 again.
def fun(b): print(id(b)) #3158044469552 b+=1; print(id(b)) #3158044469584, the address pointed to by local variable b has changed
python caches integers. An integer is like an object in memory, and only one copy is stored
a=1 b=1 print(a is b) #TRUE
Thinking: how does python do memory garbage collection
II Parameter passing in python
It can be seen from the above that unlike java, python only supports parameter passing by reference,
However, reference passing can be divided into two cases
1. Pass immutable and mutable objects
As shown in the above example, an immutable object does not change the value of the object or the direction of the argument in the function. The effect is similar to c + + value passing.
When passing variable objects, it is similar to c + + reference passing.
2. Parameter form
Function transfer in python can be divided into the following four types according to the form of parameters
Location transfer: the most common form, in which arguments and formal parameters correspond one by one.
Default parameter: when the corresponding parameter is not passed in the actual parameter, the variable uses the default value. Some default parameters can also be provided out of order when writing formal parameters. When some default parameters are not provided in order, you need to write the parameter name, threading Thread is this form.
def fun(a=0,b=0,c=0,d=0): #Default value print(a) #0 print(b) #2 print(c) #0 print(d) #1 fun(b=2,d=1)
Variable length parameter: it is also called variable parameter. In java, there is also such a parameter transfer form. In the formal parameter, the variable length parameter is a tuple
def fun(*a): for i in range(len(a)): print (a[i]) fun(1,2,3,4,5)
Keyword parameter: the formal parameter is marked with two asterisks. In the formal parameter, it is a dict, which is an extension of the variable length parameter.
def fun(a,**dic): print(a) #1 print(dic['c']) #3 fun(1,b=2,c=3)
Naming keyword parameters: it's not clear what use this form has
def fun(a,*,b,c): print(a) print(b) print(c) fun(1,b=2,c=3)
Think about what problems will arise and what principles should be followed when these methods are mixed
III Advanced function
In python, the function itself is also an object, which can be used as a parameter and a return value.
1. Function as parameter
def add(a,b): return a+b def exec(func,*args): a=0 for i in args: a = func(a ,i ) return a a = exec(add,1,2,3,4,5,6) #add is a function as a formal parameter print(a) #21
2. Function as return value
def exec(*args): def fun(): #Another function is defined in the function a=0 for i in args: a+=i return a return fun a = exec(1,2,3,4,5,6) #The return value is not an integer. A is a function print(a) # A is a function, 21 cannot be printed here, and the code in a has not been executed print(a()) #21. Here, the internal code of a is executed for the first time
3. Anonymous function
lambda expressions replace the function of anonymous inner classes in Java, which is also available in java8.
Anonymous function itself is also a function object, which is often used as formal parameters
Compared with java's anonymous inner class, anonymous functions have a limitation that they can only have one expression.
def exec(listener,a,b): if a>b: listener(a) else: listener(b) exec(lambda x:print(x),345,654)
IV Magic function
Magic function is a high-level syntax of Python, which allows you to override some fixed functions in classes, which will be called by the interpreter under specific circumstances. For example, customize in class A__ init__ () function, it will be called automatically when the object is created__ init__ () function. The format of function name is generally__ xx__ (double underline)
str() is like toString in java; hash() is the hashCode in java; wait
Copy some common magic functions here:
name()
call(), the object or instance can be called like a function, just like executing an ordinary function
new()
slots()
len()
del_ (), destructor
class obj: def __call__(self,a,b): print("__call__()",a,b) o = obj() o("Object","When the function is used")
Not all magic functions in python are defined in object. Think: when the compiler dares to call a class, there may not be some functions
V Dynamic language type
In python, there is no parent class reference pointing to a child class object.
When using java, we often let several different classes implement the same interface, and then use the reference of this interface type to point to different objects and call functions directly. So how to express this polymorphism in python, duck type.
If you search the type of duck on the Internet, you will see such a description: when you see a bird walking like a duck, swimming like a duck and barking like a duck, then the bird can be called a duck.
An object has the method I need. I don't care whether this method is inherited or not.
class cat: def run(self): print("Kitten run") class dog: def run(self): print("Dog run") def animalRun(animal): animal.run() #If you pass in an object without a run() method, an error will be reported here c = cat() d = dog() animalRun(c) animalRun(d)
Used to writing java, I think it's a little hasty. This is probably the difference between dynamic languages.
In python, the words function and method are different,
The inspect module in python provides functions ismethod() and isfunction(), judgment methods and judgment functions. I'm blinded here. Aren't methods and functions the same thing? Thinking: what is the significance of distinguishing between methods and functions in python?