Root of all evil - Advanced Functions

Before ⽅ high-energy - the main content of this section Advanced functions:

  1. Function Parameters - dynamic parameter passing
  2. Namespace, local name space, global namespace, for Use with domain, load order.
  3. Nested functions
  4. gloabal, nonlocal keyword
    ⼀ a function parameter - the dynamic mass participation
    before we said before the mass participation, if we need to ⼀ a function parameter passing, ⽽ and parameters separate warranty is uncertain or give me a ⼀.
    function pass a lot of parameters, the parameter I would write a lot, very ⿇ trouble, how do we consider using the dynamic parameters of the third parameter: dynamic parameters
    dynamic parameters is divided into two types:
  5. Dynamic receive location parameters
    therefore especially First, let's review ⼀ about the location parameters, location parameters, in accordance with the position ⾏ trekking into the mass participation
    Now the question comes up. I want to eat any food. The number is arbitrary amount of food is also arbitrary. This use when we will use the dynamic parameters of.
    writing position in the parameter list displayed shows receive any content
    def chi (quality_food, junk_food): print ( " I want to eat", quality_food, junk_food)
    Chi ( "zoomed ⽶ meal" "⼩ rice") # "zoomed rice" to the quality_food "⼩ rice" to the position in accordance with junk_food pass
    DEF Chi (
    Food): Print ( "I want to eat," Food)
    Chi ( "zoomed rice", "rice ⼩" )
    result:
    I want to eat ( 'zoomed big ⽶ rice', '⼩ small ⽶ rice') # number of parameters passed in the received content is a tuple tuple.
    dynamically receive parameters of time to pay attention: a dynamic parameter must be in the position parameter after ⾯ face
    DEF Chi ( Food, A, b): Print ( "I want to eat," Food, A, b)
    Chi ( "zoomed big ⽶ rice", "⼩ small ⽶ rice", "⻩ ⽠ yellow melon" "Submenu eggplant child")
    time trekking when running a program error. since all parameters before ⾯ surface positions have been passed in
    food received a.a and b are not always received parameters

Traceback (most recent call last):
File "/Users/sylar/PycharmProjects/oldboy/fun.py", line 95, in
Chi ( "zoomed large ⽶ rice", "⼩ small ⽶ rice", "⻩ yellow ⽠ melon", "eggplant Submenu child")
TypeError: Chi () Missing 2 required keyword-only arguments: 'A' and 'B'
so must be formulated in the following code:
this time a and b have a value, but write it position parameters can not be erased so we spent the first to write positional parameters, then using the dynamic parameters.
that default value of the parameter it ?
we find that the default value of the parameter is written before the dynamic parameters ⾯ face. the default value is only ⼀ case may ⽣ take effect.
this time we found all the default values are ⽣ take effect down the road. this time if you do not pass parameters given keyword so your default is always ⽣ force.
order: positional parameters, dynamic parameters * the default value of the parameter

  1. Dynamic receive keyword parameters
    can be dynamically position parameters in python, but this situation can only receive location parameters ⽆ not receive keyword arguments.
    DEF Chi (
    Food, A, b): Print ( "I want to eat", food, A, b)
    Chi ( "zoomed big ⽶ rice", "⼩ small ⽶ rice", a = "⻩ ⽠ yellow melon", b = "eggplant sub-submenus") # using keyword parameters must be specified
    def chi (a , b, Food): Print ( "I want to eat," A, b, Food)
    Chi ( "zoomed big ⽶ rice", "⼩ small ⽶ rice", "bread", "⾯ noodles") # The first two parameters use receives positional parameters, face parameters ⾯ using dynamic parameters received
    def chi (a, b, c = ' bread',
    Food): Print (a, B, C, Food)
    Chi ( "⾹ banana" "pineapple") # ⾹ banana pineapple bread (). The default value ⽣ entry into force of
    chi ( "⾹ banana", "pineapple", "gourd doll") # ⾹ banana pineapple gourd doll () defaults, no, no ⽣ take effect
    chi ( "⾹ bananas, "" pineapple "," gourd doll "," mask-connector ") # ⾹ pineapple banana baby gourd ( 'mask-connector') default value does not take effect ⽣
    def chi (a, b,* food, c = "Wahaha"): Print (A, B, Food, C)
    Chi ( "⾹ banana", "Pineapple") Pineapple Banana # ⾹ () Default ⽣ commencement wahaha
    chi ( "⾹ banana", "pineapple", "gourd doll") # ⾹ banana pineapple ( 'gourd baby') Wahaha defaults ⽣ entry into force of chi ( "⾹ banana", "pineapple", "gourd doll", "-connector mask ") # ⾹ banana pineapple ( 'gourd baby', 'mask-connector') entered into force Wahaha defaults ⽣

Using the python with the manipulation received Dynamic keyword parameter
DEF FUNC (
kwargs):
Print (kwargs)
FUNC (A =. 1, B = 2, C =. 3)
FUNC (A =. 1, B = 2)
Results:
{ ' a ': 1,' b ' : 2,' c ': 3} {' a ': 1,' b ': 2}
the time of reception is ⼀ a dict
issue order, when a function call using used, If the first key parameter is given, the entire column of the parameter list given.
Therefore, key parameters must ⾯ surface after the position parameter. Since the argument that order. Therefore, when the parameter is received in this order. that position parameters ⾯ surface must also dynamic parameters after receiving a keyword before the keyword parameter ⾯ surface.
the final sequence ( ):
position parameter>
args> default parameters> ** kwargs these four parameters can be arbitrarily into ⾏ row exercise by using .
If you want to receive all parameters:
another ⼀ one kind of dynamic parameters passed parameters ⽅ manner:
DEF FUNC (a, B, C, D):
Print (a, B, C, D)

Keyword arguments must ⾯ face after positional parameters, or parameters chaos func (1, 2, c = 3, 4)

FUNC DEF (* args, ** kwargs):
Print (args, kwargs)
FUNC ( "Twisted Vine," "⻢ horse halo", wtf = "Hu soup")
DEF Fun ( args):
Print (args)
LST = [. 1,. 4,. 7]
Fun (LST [0], LST [. 1], LST [2])
Fun (
LST) # can use the use the columns in sequence listing ⼀ a broken s = "do not ⾂ Chenqie not "
Fun (
S) # string string may be broken, (iterables)
to a sequence listed in ⼀ argument position, the list of columns, the object may be an iterative front surface ⾯ * add this sequence are shown in table PLAY column in order to break up.

Parameter in position on the displayed table shows the composition * received parameters into a tuple if ⼀ ⼀ a dictionary, but it needs to be broken up by two Use *
NOTE function:
DEF Fun ( kwargs) :
Print (kwargs)
DIC = { 'a':. 1, 'B': 2}
Fun (
DIC)
DEF Chi (Food, Drink):
"" "
individual cases from where there is a comment function to write ⼀ about this that What is the function of what dry, dry, ⽐ example, I eat this function is ⼀: param food: food is not even what it means parameter
: param drink: What parameters drink is what it means
: return: What is the return of what stuff
. "" "
Print (Food,. Drink.)
return "Very Good"
⼆ II. namespace
after the python interpreter began to perform trekking, ⼀ it will open up a space in memory, whenever encountered ⼀ a variable amount, on
the the relationship between the amount of variable names and values recorded, but when faced with the function definition, function name interpreter just read into memory, displayed table shows the function of the present, as for ⾄ variable volume and internal logic functions, explained center Weighted is not related to the heart. that ⼀ the beginning of time function just loaded in, This ⽽ it only when the called function Use with and access, the interpreter will be based on the amount of variables declared within a function to open up the interior space into ⾏ trekking variable amount. With the completion of trekking function Perform these functions using the internal variable amount of space will be used as a function perform trekking finished ⽽ be emptied.
We give the name of storage space relationships and values from ⼀ a name: namespace variable amount in our time memory is stored in this space
namespace Category:

  1. Global namespace -> Our py files directly in the file, the variable declared outside a function of the amount belongs to the global namespace
  2. Local namespace -> amount of variables declared in the function will be placed on local namespace
  3. Built-in namespace -> storage python interpreter provides us with the name, list, tuple, str, int these are within the
    set namespace
    load order:
  4. Built namespace
    DEF Fun ():
    A = 10
    Print (A)
    Fun ()
    Print (A) # A does not already exist ..

  5. Global namespace
  6. (When the function is YES trekking) local namespace
    values order:
  7. 2. The local global namespace namespace namespace 3. Built
    for Use by Field: Use as a field is used as a range Use according ⽣ commencement point of view divided into global and local fields for Use with a domain for Use
    as Global Use by domain: contains built-namespace and global namespace at any location throughout the file can use the file are used (from top to bottom followed by perform ⾏ trekking trekking) for use with the local domain: can be internal function use with.
    for use with the domain name space:
  8. Use the domain for global use: built-in global namespace + local name space for 2. Use a domain: local namespace
    we can see by the global globals () function for Use with domain content, it can be viewed by locals () use as a local variable domain of the function information and the amount of
    a = 10
    DEF FUNC ():
    a = 20 is
    Print (a)
    FUNC () # 20 is
    a = 10
    DEF FUNC ():
    a = 40
    B = 20 is
    DEF ABC () :
    Print ( "ha")
    Print (A, B)
    Print (Globals ())
    Print (about locals ())
    FUNC ()

    Using individual cases from ri used locally for Use with domain

    # Use print content for global use domain local print content for Use with domain

    III. Nested functions
  9. As long as the event ⻅ met with the () is the transfer function Use with if there is no () will not function with transfer Use Perform trekking 2. order function.
    DEF fun1 ():
    Print (111)

def fun2():
print(222)
fun1()
fun2()
print(111)

Def nested function fun2 ():

print(222)
def fun3():
    print(666)
print(444)

fun3 ()
Print (888)
Print (33)
fun2 ()
Print (555)
IV. keyword global and nonlocal
therefore especially ⼀ First, we write such a code therefore especially first ⼀ a variable amount in a global statement, then with local dimming of Use this variable quantity, and this change
variable value amount
global monitor shown in table. using no content for topical use use of the domain. ⽽ and global change for use with a variable domain use amount of
a = 100
FUNC DEF ():
Global A
A = 28
Print (A)
FUNC ()
Print (A)

Plus had a global means no longer create the local variables of the amount. ⽽ but directly Using the use of a global

lst = [ "twist vine", "Carina", "James"] DEF FUNC ():
lst.append ( "⻢ Ma Yunyun") of the assignment does not.
Print (LST)
FUNC ()
Print (LST)

For variable data types can be accessed directly into ⾏ trekking, but does not address can be changed. White White said

Table nonlocal displayed as shown in topical Using a domain name Use amount adjusting variable space with ⽗ parent.
A = 10
DEF func1 ():
A = 20 is
DEF func2 ():

    nonlocal a
    a = 30
    print(a)
func2()
print(a)

func1 ()
Results: 30 plus a nonlocal
30 does not add 30 nonlocal
20 is
again, if a nested many layers, what effect would be one kind even ⼀:
A. 1 =
DEF fun_1 ():
A = 2
DEF fun_2 ():
nonlocal AA =. 3
DEF fun_3 ():
A =. 4
Print (A)
Print (A)
fun_3 ()
Print (A)
Print (A)
fun_2 ()
Print (A)
Print (A)
fun_1 ()
Print ( a)
such a program can analyze if clear white white. so for use with no problem domain, global, nonlocal of the

Guess you like

Origin www.cnblogs.com/yet-320/p/11026435.html