Function defines a method in Python

  • 2020-04-02 09:32:41
  • OfStack

Let's first define a function:

def foo(): 
print('function') 
foo() 

In the code above, a function named foo is defined that takes no arguments. The last line of code calls this function. This is the simplest form of a function. Let's introduce the functions with parameters:

def foo(): 
print('function') 
def foo1(a,b): 
print(a+b) 
foo() 
foo1(1,2)

Foo1 is a function with arguments, and you can call this function with arguments using foo1(1,2).

When a variable exists in a program, the scope of the variable is involved. In Python, variables are scoped at three levels: global, local, and nonlocal.

Global: as the name implies, represents a global variable. That is, the variable is at the highest level in python, which means that the variable is defined at the highest level, not in a function or class.
Local: local variable, defined in a function.
Nonlocal: this is a relative concept. In python, function internals can be nested to define internal functions, so that the variables inside the function are nonlocal relative to the nested functions inside the function.
Next, give the relevant procedures to explain, first look at the global and local variables:

x = 1 
y = 2 
def foo(x): 
print(x) 
print(y) 
print('***********') 
x = 3 
global y 
y = 3 
print(x) 
print(y) 
print('***********') 
foo(x) 
print(x) 
print(y) 

#************************ 
# The results  
1 
2 
*********** 
3 
3 
*********** 
1 
3

In the above program, two global variables x and y are defined, and inside the function foo, a local variable x is also defined. According to the results of the run, inside foo, the variable x is the true local variable. Because the changes made to it don't affect the global variable x. Also, if you need to use global variables inside foo, you need to use the global keyword. The intention of global y is to declare the variable y as the externally declared global variable y. So if you change y inside foo, you still have an effect outside foo. Because foo is modifying a global variable.
And nonlocal:
 
def out(): 
z = 3 
def inner(): 
nonlocal z 
z = 4 
print('inner function and z = {0}'.format(z)) 
inner() 
print('out function and z = {0}'.format(z)) 
out() 
#********** 
# The results  
inner function and z = 4 
out function and z = 4 


For the variable z, it is nonlocal. If you need to use z in inner, you need to declare it using the nonlocal keyword.
Now that we've covered the scope of variables, it's time to talk about the parameters of functions. Functions in Python can take 0-n arguments, which is no more special than any other programming language. Again, you can specify default values for function arguments. Such as:
 
def power(num,power = 1): 
print(num ** power) 
power(2) 
power(2,2) 
#************* 
# The results  
2 
4 

For the first call to power(2), the value of the second parameter, power, is not specified, and from the output, the function USES the default value. The second time power(2,2) is called, because the value of the second parameter is given, the value of power is 2.
The important thing to note here is that in python, for arguments that give you default values, be sure to be at the end of the argument list. That is, if a parameter with a given default value appears in the parameter list, no parameter without a given default value can appear.
For parameter assignments, you can use a keyword. Such as:
 
def func(a,b=1,c=2): 
print('a={0},b={1},c={2}'.format(a,b,c)) 
func(1) 
func(1,2) 
func(1,c=3) 
func(b=2,a=4) 
#************* 
# The results  
a=1,b=1,c=2 
a=1,b=2,c=2 
a=1,b=1,c=3 
a=4,b=2,c=2 

You can assign a parameter by specifying the parameter name.
Python supports a list of variable arguments. First, let's look at the following program:
 
def total(initial=5,*numbers,**keywords): 
count = initial 
for number in numbers: 
count += number 
for key in keywords: 
count += keywords[key] 
return count 
print(total(10,1,2,3,vegetables=10,fruits=5)) 
#*************** 
# The results  
31 

In the above program, the noteworthy are * Numbers and **keywords. In the program, the first parameter 10 corresponds to the first initial parameter. Next comes the list of variables: 1,2,3 is a sequence, vegetables=10,fruits=5 is a dictionary. In Python, * Numbers means: put 1,2,3 or more parameters into the sequence; **keywords means: put parameters such as vegetables=10 or more into a dictionary.

Related articles: