Python objects, methods, classes, instances, function Usage Analysis, and python Functions
This article analyzes Python objects, methods, classes, instances, and function usage. Share it with you for your reference. The specific analysis is as follows:
What is a recursive function?Inside the function, you can also continue to call other functions, if a function calls itself internally, this function is a recursive functionGive an example of a factorial that asks N:def fact (n): if n = = 1:return 1; Else : return N * Fact (
, 2) C2/>mixarg (1, 2, c = 3)
Mixarg (1, 2, 3, ' A ', ' B ')
Mixarg (1, 2, 3, ' A ', ' B ', x =)
args = (1, 2, 3, 4)
kw = {' X ': Mixarg}
(*args, **kw)Output
A = 1 b = 2 c = 0 args = () kw = {}
a = 1 b = 2 c = 3 args = () kw = {}
a = 1 b = 2 c = 3 args = (' A ', ' b ') kw = {
A = 1 b = 2 c = 3 args = (' A ', ' b ') kw = {' X ': n '
a = 1 b = 2 c = 3 args = (4,) kw = {' X ': 98}Summary: Required parameters, default parameters, variable parameters, keyword parameters can be used in any combinatio
the Deferred object is activated due to a result or error, so that the Getter instance will no longer hold a reference to the Deferred object to be activated. This has several advantages. First, it can avoid the possibility that Getter. gotResults may repeatedly activate the same Deferred object (this will lead to an AlreadyCalledError exception ). In this way, a callback function that calls the Getter. ge
variables can receive a tuple at the same time, by the location of the corresponding value, so the Python function returns a multi-value is actually returned a tuple, but it is more convenient to write.
Recursive functions:
Inside a function, you can call other functions. If a function
of a variable parameter and keyword parameter when calling a function:
Variable parameters can be directly passed in: Func (1, 2, 3), can be assembled first list or tuple, and then passed through the *args: Func (* (1, 2, 3));
Keyword parameters can be passed directly into: Func (A=1, b=2), can be assembled dict first, and then passed **kw to: func (**{' a ': 1, ' B ': 2}).
Using *args and **kw is a Python
*Self.height the Print('length =', Self.length,'width =', Self.width,'height =', Self.height,'valum=', self.__valum) - - defInfo_color (self): - #Self.set_color (Self._color) #在类中, the way function calls functions + Print('The color of box is', Self._color) - + defOpen_box (self): A ifbox.openstate==0: at Print('Open the box.') -Box.openstate=1 -
not need to provide age and city when registering, providing only two parameters that are required:
>>> enroll (' Sarah ', ' F ')
name:sarah
gender:f
age:6
city:beijing
Only students who are not in conformity with the default parameters need to provide additional information:
Enroll (' Bob ', ' m ', 7)
enroll (' Adam ', ' m ', city= ' Tianjin ')
As you can see, the default parameters reduce the difficulty of the function call, and when more compl
definition function uses the DEF keyword, and the general format is as follows:def function name (argument list): function bodyBy default, parameter values and parameter names are matched in the order defined in the function declaration.#!/usr/bin/python3# Calculate area Functi
argument is valid in the process of a function call, and it is not valid after it!Angle of argument:Value My_sum by locationValue by keyword my_sum (y=1,x=2)Mixed transfer value my_sum (1,y=2)From the formal parameter angle:Positional parameters: def foo (x, Y, z) (more function changes)Default parameters: Def foo (x,y=1) (the function is not changed much, the n
nth) call to the generator function jumps to the middle of the function, and all local variables that were last called remain unchanged.2. Features:1. The generator is a function, and the parameters of the function are preserved.2. When iterating to the next call, the parameters used are left for the first time, that
. In this example, printtwice is called by cattwice, while cattwice is called by main. _ Main is the special name used to represent the entire stack graph. When you create a variable outside of all functions, it belongs to _ main.Each form parameter points to the same value as its corresponding real parameter. Therefore, the values of part1 and line1 are the same, the values of part2 and line2 are the same, and the values of bruce and cat are the same.If an error occurs during the
, such as power (5, 3), must be explicitly input ).
As shown in the preceding example, the default parameters can simplify function calls. Note the following when setting default parameters:
First, the required parameter is in the front, and the default parameter is in the back; otherwise, the Python interpreter reports an error (think about why the default param
passed by reference. If you modify the parameters in the function, the original parameters are changed in the function that called the function. For example:Example 2:#!/usr/bin/env python#-*-coding:utf-8-*-#@Time: 2016/9/22 22:29#@Author: WwyxdefChange_var (mylist):"Changing list variables"Mylist.append ([1, 2, 3])
slow then you definitely need to add. Is that what it is all about. Not really, so this has the following knowledge points we need to talk about, several different types of waiting. 9.2 Force-waiting 9.2.1 What is forced wait
Therefore, the name of righteousness is that you must give me a little bullying meaning. For example: I entered the landing page, just have a mandatory waiting function, then the result is no matter whether the resources of the
Looking at learn Python recently, see the effect of using function properties to accomplish nonlocal.
def test (start):
def Nest (label):
print (label,nest.state)
nest.state+=1
Nest.state=start
Return Nest
Personally think this is typographical error. Should be as follows:
def test (start):
def Nest (label):
print (label,nest.state)
nest.state+=1
Nest.state=start
Return NestBut t
functionname (parameters):
"Function _ Document String"
Function_suite
return [expression]
By default, parameter values and parameter names are matched in the order defined in the function declaration.
Instance
Here is a simple Python function that takes a string as an incoming parameter and prints it to a standard
Namespaces and Scopesvariables, functionsDivided into three kinds#内置名称空间 built-in functions,System functions are internally customized.Python view built-in functions, commands:Import BuiltinsDir (builtins)#全局名称空间 file is written in the head, there is no indentation defined function or variable.Once defined, it can be used anywhere in the entire file.#局部名称空间Once defined, it can be called within a local environment, such as a variable defined in a defin
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.