In Python, a type belongs to an object, and a variable is of no type:
A=[1,2,3]a= "Runoob"
In the above code, [All-in-one] is a list type,"Runoob" is a string type, and the variable A is no type, she is simply a reference to an object (a pointer), can be a list type object, or can point to a string type Object.
Can change (mutable) and non-changing (immutable) objects
In Python, strings, tuples, and numbers are objects that cannot be changed, and List,dict are objects that can be modified.
immutable Type: variable assignment a=5 and then assign value a=10, here is actually reborn into an int value object 10, then a point to it, and 5 is discarded, not change the value of a, the equivalent of a new student into a.
Variable type: variable assignment la=[1,2,3,4] after assigning a value la[2]=5 is to change the value of the third element of List LA, itself LA is not moving, but its internal part of the value has been modified.
Parameter passing of the Python function:
immutable types: values such as C + + are passed, such as integers, strings, and tuples. such as fun (a), passing only a value, does not affect the A object itself. For example, in Fun (a) to modify the value of a, just modify another copy of the object, does not affect the a itself.
mutable types: references such as C + + are passed, such as lists, dictionaries. such as Fun (LA), is the real transfer of LA, the modified fun outside of LA will also be affected
Everything in Python is an object, strictly meaning we can't say value passing or reference passing, we should say immutable objects and pass mutable objects.
Python-Pass Immutable object instance
#!/usr/bin/python#-*-coding:utf-8-*-def changeint (a): a = 10b = 2ChangeInt (b) Print B # result is 2
The instance has int object 2, the variable that points to it is B, and when passed to the Changeint function, the variable b,a and B both point to the same int object, and at a=10, the new int value Object 10, and a points to it.
Passing a Mutable object instance
#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def changeme (mylist): "Modify incoming list" mylist.append ([1,2,3,4]); Print "Function inside value:", MyList return # call changeme function mylist = [10,20,30];changeme (mylist);p rint "function outside the value:", MyList
The same reference is used for the object passing in the function in the instance and adding new content at the end, so the output is as follows:
Values in function: [10, 20, 30, [1, 2, 3, 4]] values outside the function: [10, 20, 30, [1, 2, 3, 4]]
Parameters
The following are the formal parameter types that can be used when calling a function:
- Required Parameters
- Keyword parameters
- Default parameters
- Indefinite length parameter
Required Parameters
The required parameters must be passed into the function in the correct order. The number of calls must be the same as when declared.
Call the Printme () function, you must pass in a parameter, or a syntax error will occur:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme ();
The result of the above example output:
Traceback (most recent): File "test.py", line one, in <module> printme (); Typeerror:printme () takes exactly 1 argument (0 given)
Keyword parameters
Keyword arguments are closely related to function calls, and function calls use keyword parameters to determine the values of the parameters passed in.
Using the keyword argument allows a function call when the order of the arguments is inconsistent with the Declaration, because the Python interpreter can match the parameter values with the name of the argument.
The following instance uses the parameter name when the function Printme () is called:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme (str = "My string");
The result of the above example output:
My string
The following example shows that the keyword parameter order is not important to show more clearly:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age): "Print any incoming string" Print "name:", name;
print "age", age; Return #调用printinfo函数printinfo (age=50, name= "Miki");
The result of the above example output:
Name: mikiage 50
Default parameters
When a function is called, the value of the default parameter is considered to be the default value if it is not passed in. The following example prints the default age if age is not passed in:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age = +): "Print any incoming string" Print "name:", Nam e; Print "Age", age; Return #调用printinfo函数printinfo (age=50, name= "Miki");p Rintinfo (name= "Miki");
The result of the above example output:
Name: mikiage 50Name: mikiage 35
Indefinite length parameter
You may need a function that can handle more arguments than was originally declared. These parameters are called indeterminate length parameters, and are not named when declared, unlike the above 2 parameters. The basic syntax is as follows:
def functionname ([Formal_args,] *var_args_tuple): "Function _ Document String" function_suite return [expression]
Variable names with an asterisk (*) will hold all unnamed variable arguments. Examples of indefinite length parameters are as follows:
#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def printinfo (arg1, *vartuple): "Prints any incoming parameter" print "output:" PR int arg1 for var in vartuple: print var return; # call printinfo function Printinfo (70, 60, 50);p Rintinfo
The result of the above example output:
Output: 10 Output: 706050
Parameter passing
In Python, a type belongs to an object, and a variable is of no type:
A=[1,2,3]a= "Runoob"
In the above code, [All-in-one] is a list type,"Runoob" is a String type, and variable A is no type, she is simply a reference to an object (a pointer), either a list type object, or a string The type object.
Can change (mutable) and non-changing (immutable) objects
In Python, strings, tuples, and numbers are objects that cannot be changed, and List,dict are objects that can be modified.
immutable Type: variable assignment a=5 and then assign value a=10, here is actually reborn into an int value object 10, then a point to it, and 5 is discarded, not change the value of a, the equivalent of a new student into a.
Variable type: variable assignment la=[1,2,3,4] after assigning a value la[2]=5 is to change the value of the third element of List LA, itself LA is not moving, but its internal part of the value has been modified.
Parameter passing of the Python function:
immutable types: values such as C + + are passed, such as integers, strings, and tuples. such as fun (a), passing only a value, does not affect the A object itself. For example, in Fun (a) to modify the value of a, just modify another copy of the object, does not affect the a itself.
mutable types: references such as C + + are passed, such as lists, dictionaries. such as Fun (LA), is the real transfer of LA, the modified fun outside of LA will also be affected
Everything in Python is an object, strictly meaning we can't say value passing or reference passing, we should say immutable objects and pass mutable objects.
Python-Pass Immutable object instance
#!/usr/bin/python#-*-coding:utf-8-*-def changeint (a): a = 10b = 2ChangeInt (b) Print B # result is 2
The instance has int object 2, the variable that points to it is B, and when passed to the Changeint function, the variable b,a and B both point to the same int object, and at a=10, the new int value Object 10, and a points to it.
Passing a Mutable object instance
#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def changeme (mylist): "Modify incoming list" mylist.append ([1,2,3,4]); Print "Function inside value:", MyList return # call changeme function mylist = [10,20,30];changeme (mylist);p rint "function outside the value:", MyList
The same reference is used for the object passing in the function in the instance and adding new content at the end, so the output is as follows:
Values in function: [10, 20, 30, [1, 2, 3, 4]] values outside the function: [10, 20, 30, [1, 2, 3, 4]]
Parameters
The following are the formal parameter types that can be used when calling a function:
- Required Parameters
- Keyword parameters
- Default parameters
- Indefinite length parameter
Required Parameters
The required parameters must be passed into the function in the correct order. The number of calls must be the same as when declared.
Call the Printme () function, you must pass in a parameter, or a syntax error will occur:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme ();
The result of the above example output:
Traceback (most recent): File "test.py", line one, in <module> printme (); Typeerror:printme () takes exactly 1 argument (0 given)
Keyword parameters
Keyword arguments are closely related to function calls, and function calls use keyword parameters to determine the values of the parameters passed in.
Using the keyword argument allows a function call when the order of the arguments is inconsistent with the Declaration, because the Python interpreter can match the parameter values with the name of the argument.
The following instance uses the parameter name when the function Printme () is called:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme (str = "My string");
The result of the above example output:
My string
The following example shows that the keyword parameter order is not important to show more clearly:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age): "Print any incoming string" Print "name:", name;
print "age", age; Return #调用printinfo函数printinfo (age=50, name= "Miki");
The result of the above example output:
Name: mikiage 50
Default parameters
When a function is called, the value of the default parameter is considered to be the default value if it is not passed in. The following example prints the default age if age is not passed in:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age = +): "Print any incoming string" Print "name:", name ; Print "Age", age; Return #调用printinfo函数printinfo (age=50, name= "Miki");p Rintinfo (name= "Miki");
The result of the above example output:
Name: mikiage 50Name: mikiage 35
Indefinite length parameter
You may need a function that can handle more arguments than was originally declared. These parameters are called indeterminate length parameters, and are not named when declared, unlike the above 2 parameters. The basic syntax is as follows:
def functionname ([Formal_args,] *var_args_tuple): "Function _ Document String" function_suite return [expression]
Variable names with an asterisk (*) will hold all unnamed variable arguments. Examples of indefinite length parameters are as follows:
#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def printinfo (arg1, *vartuple): "Prints any incoming parameter" print "output:"
print arg1 for var in vartuple: print var return; # call printinfo function Printinfo (70, 60, 50);p Rintinfo
The result of the above example output:
Output: 10 Output: 706050
Parameter passing
In Python, a type belongs to an object, and a variable is of no type:
A=[1,2,3]a= "Runoob"
In the above code, [All-in-one] is a list type,"Runoob" is a string type, and the variable A is no type, she is simply a reference to an object (a pointer), can be a list type object, or can point to a string type Object.
Can change (mutable) and non-changing (immutable) objects
In Python, strings, tuples, and numbers are objects that cannot be changed, and List,dict are objects that can be modified.
immutable Type: variable assignment a=5 and then assign value a=10, here is actually reborn into an int value object 10, then a point to it, and 5 is discarded, not change the value of a, the equivalent of a new student into a.
Variable type: variable assignment la=[1,2,3,4] after assigning a value la[2]=5 is to change the value of the third element of List LA, itself LA is not moving, but its internal part of the value has been modified.
Parameter passing of the Python function:
immutable types: values such as C + + are passed, such as integers, strings, and tuples. such as fun (a), passing only a value, does not affect the A object itself. For example, in Fun (a) to modify the value of a, just modify another copy of the object, does not affect the a itself.
mutable types: references such as C + + are passed, such as lists, dictionaries. such as Fun (LA), is the real transfer of LA, the modified fun outside of LA will also be affected
Everything in Python is an object, strictly meaning we can't say value passing or reference passing, we should say immutable objects and pass mutable objects.
Python-Pass Immutable object instance
#!/usr/bin/python#-*-coding:utf-8-*-def changeint (a): a = 10b = 2ChangeInt (b) Print B # result is 2
The instance has int object 2, the variable that points to it is B, and when passed to the Changeint function, the variable b,a and B both point to the same int object, and at a=10, the new int value Object 10, and a points to it.
Passing a Mutable object instance
#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def changeme (mylist): "Modify incoming list" mylist.append ([1,2,3,4]); Print "Function inside value:", MyList return # call changeme function mylist = [10,20,30];changeme (mylist);p rint "function outside the value:", MyList
The same reference is used for the object passing in the function in the instance and adding new content at the end, so the output is as follows:
Values in function: [10, 20, 30, [1, 2, 3, 4]] values outside the function: [10, 20, 30, [1, 2, 3, 4]]
Parameters
The following are the formal parameter types that can be used when calling a function:
- Required Parameters
- Keyword parameters
- Default parameters
- Indefinite length parameter
Required Parameters
The required parameters must be passed into the function in the correct order. The number of calls must be the same as when declared.
Call the Printme () function, you must pass in a parameter, or a syntax error will occur:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme ();
The result of the above example output:
Traceback (most recent): File "test.py", line one, in <module> printme (); Typeerror:printme () takes exactly 1 argument (0 given)
Keyword parameters
Keyword arguments are closely related to function calls, and function calls use keyword parameters to determine the values of the parameters passed in.
Using the keyword argument allows a function call when the order of the arguments is inconsistent with the Declaration, because the Python interpreter can match the parameter values with the name of the argument.
The following instance uses the parameter name when the function Printme () is called:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printme (str): "Print any incoming string" Print str; Return #调用printme函数printme (str = "My string");
The result of the above example output:
My string
The following example shows that the keyword parameter order is not important to show more clearly:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age): "Print any incoming string" Print "name:", name;
print "age", age; Return #调用printinfo函数printinfo (age=50, name= "Miki");
The result of the above example output:
Name: mikiage 50
Default parameters
When a function is called, the value of the default parameter is considered to be the default value if it is not passed in. The following example prints the default age if age is not passed in:
#!/usr/bin/python#-*-coding:utf-8-*-#可写函数说明def printinfo (name, age = +): "Print any incoming string" Print "name:", Nam e; Print "Age", age; Return #调用printinfo函数printinfo (age=50, name= "Miki");p Rintinfo (name= "Miki");
The result of the above example output:
Name: mikiage 50Name: mikiage 35
Indefinite length parameter
You may need a function that can handle more arguments than was originally declared. These parameters are called indeterminate length parameters, and are not named when declared, unlike the above 2 parameters. The basic syntax is as follows:
def functionname ([Formal_args,] *var_args_tuple): "Function _ Document String" function_suite return [expression]
Variable names with an asterisk (*) will hold all unnamed variable arguments. Examples of indefinite length parameters are as follows:
#!/usr/bin/python#-*-coding:utf-8-*-# writable function Description def printinfo (arg1, *vartuple): "Prints any incoming parameter" print "output:" PR int arg1 for var in vartuple: print var return; # call printinfo function Printinfo (70, 60, 50);p Rintinfo
The result of the above example output:
Output: 10 Output: 706050
PYTHON parameter passing