external function, and the variables of the external function always exist, let's take a look at the example below. deffunc (): a=0Print('output variables for external functions:', a)defF1 ():Print('internal function Output variables:', a)returnf1f=func () f ()#The first call prints out "variables for output external functions: 0" and "intrinsic
that lst is a number set and has a sorting method, sort needs to determine the order as a parameter.If a function cannot be used as a parameter, the sort method of LST can only accept common objects as parameters. In this way, we need to first define an interface, then define a class that implements this interface, and finally pass an instance of this class to the sort method, which calls the compare metho
test function to decorate the In fact, the adorner does not need to execute the code in the function func, just before the end of the function add calls the function Func to get the result of the Func return and the result of the additional codeWhen the adorner itself needs
, built-in scope
4. ExamplesEx1
passline = 60def func(val): if val >= passline: print('pass') else: print('failed')func(89) '''''''''''' pass[Finished in 0.2s] ''''''''''''
The Python function first looks for local and does not have the definition of passline in the scope of local variables. then it finds that there is no built-in function in the
1. "B-mode" for file operation (supplemental)
In the last article, I wrote some of the methods of file handling in the final section, but I think it is necessary to mention the following:
Like RB, WB, AB This mode, is in the form of byte operation, need to pay attention to the following several questions:
1) files can not be saved in memory, can only be saved on the hard disk, in binary form, Python can only write stri
Python function scope (python3.5), pythonpython3.51 Basic Concept 1.1 namespace)
The namespace is the ing from the variable name to the object (name-> obj ). Currently, most namespaces are implemented in the form of a python dictionary, and the Implementation form may change in the future. Namespace example: built-in variables (built-in functions abs, built-in ex
-in Scope
4. ExamplesEx1
passline = 60def func(val): if val >= passline: print('pass') else: print('failed')func(89) '''''''''''' pass[Finished in 0.2s] ''''''''''''
The Python function first looks for local and does not have the definition of passline in the scope of local variables. Then it finds that there is no built-in function in the
to read more books and do not bury them in the pitfalls dug by questions ......
Function overloading is required for static languages mainly to solve the call flexibility problem. Using function overloading in a flexible scripting language such as Python is simply an option.
[1]: http://www.artima.com/weblogs/viewpost.jsp? Thread = 1, 101605
Do other languages
is exactly the same as the second edition of fab:
>>> for n in fab(5):... print n...
In short, yield is used to convert a function into a generator. A function with yield is no longer a common function. The Python interpreter regards it as a generator and calls fab (5) ins
parsing
Without changing the code of the China () and Japan () functions, the user authentication is added, only the authentication pass can call the function inner expression.
The code script is executed first, and Python detects the @ symbol, which will pass the corresponding function name as a parameter to the login () function's namespace
F1
necessary to explicitly pass in N, such as power (5, 3).
As you can see from the example above, the default parameters can simplify the invocation of a function. When setting default parameters, there are a few things to note:
The first is the required parameter before, the default parameter is behind, otherwise the Python interpreter will error (think about why the default parameters can not be placed in
Python learning Day 4 function slicing iterative list generator,
Define functions
Def my_abs (x): # evaluate the absolute value of the my_abs Function
If x> = 0:
Return x
Else:
Return-x
Def nop (): # Empty Function
Pass # placeholder
Parameter check
>>> My_abs (1, 2)
Traceback (most recent call last ):
File "
TypeErr
becomes a dictionary, or a dictionary (such as: info) can be passed directly into the Kwargs, but in the function call in front of the dictionary plus * * (such as **info)In addition , a non-fixed parameter cannot be placed behind a positional parameter, but the key parameter can be located behind the *args parameter. def stu_register (name,age,*args,gender): print(name,age,args,gender) stu_ Register ('jack', +,'CN','
This example describes the Python callback function usage. Share to everyone for your reference. The specific analysis is as follows:
First, the Baidu encyclopedia on the callback function explanation:
A callback function is a function that is called through a
(): names[0] = "哈哈" print("inside :",names)change_name()print(names)执行结果:inside : [‘哈哈‘, ‘Jack‘, ‘Rain‘]outside : [‘哈哈‘, ‘Jack‘, ‘Rain‘]4. Function recursion
Inside a function, you can call other functions. If a function calls itself internally, the function i
Define a function, two threads call the function, respectively, in their own stack space, each thread does not affect each other.
So from here you can see the local variable is created when it is called, the end of the call is dead (when the call is pressed, it pops up at the end, and the local variable is not referenced)
Note:1 def fn (a): 2 Print (a) 3 4 g =
of the adorner, statistics the running time without changing the method of the original function Call.1 Import time2 deftest1 (func):3 defDeco ():#run-time calculations for this function4Start_time =time.time ()5 func ()6Stop_time =time.time ()7 Print('the func Run time is%s'% (stop_time-Start_time))8 returnDeco#returns the Deco variable after the function is run9@test1#call an adorner, in
Python, the function itself is an object, in the global domain, the function object is referenced by the function name, so it is possible to refer to the function object with other variable names, which is equivalent to changing the name.Like what:def test ():Print ("Test")
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.