11th days-advanced functions

Source: Internet
Author: User

 

 

 

# Dynamically passing parameters:

# * Receives dynamic parameter passing from parameters in all locations
# When passing parameters, the real parameters are automatically packaged as the ancestor to the form parameters.
1 def Chi (* Food): 2 print (food) 3 return food 4 5 Chi () # dynamically passing parameters can be empty without passing parameters 6 # Chi ("Zi Cai Tang ", food = "") # * ARGs cannot receive keyword parameters 7 8 Chi ("", "", "") 9 Chi ("", "", "wonton soup", "kelp stew pork", "chicken stew mushroom") # returns a ancestor 10 ', 'kelp braised pork ', 'chicken braised mushroom ')

 

# ** Dynamic parameter passing for receiving keyword Parameters
# Package the real parameters into a dictionary and pass them to the form parameters
1 def chi1 (** food): 2 print (food) 3 4 chi1 (good_food = "Haha", junk_food = "Haha", drink = "") # Return a dictionary of 5 # {'good _ food': 'haha ', 'junk _ food': 'ha', 'drink ': 'giggle '} 6 # chi1 ("Haha", "Haha", "ROAR") # typeerror ** kwargs cannot receive location parameters
# Parameter order location ** ARGs default value ** kwargs can be combined as needed


# Invincible Parameters
1 # invincible parameter 2 def num (* ARGs, ** kwargs): 3 Print (ARGs, kwargs) 4 5 num (1, 2, 3, 4, 5, 6, A = "7 ", B = "8", c = "9") 6 # (1, 2, 3, 4, 5, 6) {'A': '7', 'B ': '8', 'C': '9 '}

 

1 Lis = [1, 2, 3, 4, 5, 6, 7] 2 def fun (* ARGs): # aggregate multiple 3 prints (ARGs) in the form parameter) 4 5 fun (LIS [0], Lis [1], Lis [2], Lis [3], Lis [4], Lis [5], Lis [6]) 6 fun (* LIS) # saving multiple input operations is equivalent to dividing a list, character, or ancestor into parameters and passing them to the form parameter 7 S = "October 30, 2018 19:50:39" 8 fun (* s) # indicates in the real parameter that 9 10 DIC = {"Zhang Wuji": "Yi Tian tu long Kee", "Qiao Feng": "tianlong Babu", "Guo Jing ": "Shoot sculpture"} 11 def fun1 (** kwargs): # aggregation 12 print (kwargs) 13 14 # fun1 (Zhang Wuji = DIC ['zhang Wuji '], Qiao Feng = "tianlong Babu ") 15 fun1 (** DIC) # scatter the dictionary into a keyword parameter and pass it to the form parameter. Save the preceding input for 16 17 # conclusion *, **: In the form parameter: aggregation, real parameter: scatter

 

 

# Namespace:
# Definition: the space in which names and values are stored is called a namespace.

# Built-in namespace -- Python Interpreter
# Names that can be used when the python interpreter is started are stored in the built-in namespace, such as print () input () List tuple.
# The built-in name is loaded into the memory when the interpreter starts.
# Global Command Space-written code
# The program is loaded into the memory in sequence during execution from top to bottom
# Put the variable name and function name that we set in color
# Local namespace-function classes, methods, other modules, and objects
# Name Defined inside the Function
# Space generated when a function is called is cleared after function execution.

# Local: You can use global and built-in namespace names.
# Global: built-in and local
# Built-in: local and global names are unavailable
1 # Global Call: global namespace-> built-in namespace 2 def fun (): 3 A = 10 4 print (a) 5 6 fun () # A has been cleared after function execution. 7 # print (a) # name 'A' is not defined. Local 8 9 cannot be used globally. # local call: local namespace-> global namespace-> built-in namespace 10 A = 111 def func (x): 12 x = A13 print (x) 14 15 func (10) # print 1 Local callable global 16 17 # built-in namespace: Do not call down because the load has been completed before running
# Built-in -- Global -- local (dependency inversion: reversible and not smooth)


# Loading sequence: built-in namespace (loaded before the program runs)-> global namespace (running: loaded from top to bottom)-> local namespace (running: loaded only when called)

 

 

# Scope: the scope is the scope of action. According to the effective scope, the scope is divided into global scope and local scope.

# Global scope:
# Contains the built-in namespace and global namespace, which can be used in any location of the entire file (following the execution from top to bottom line by line)

# Local scope: Can be used inside a function.

# Scope namespace:
# Global scope: global namespace + Built-in namespace
# Local scope: local namespace

# You can use the globals () function to view the content in the global scope, or you can use locals () to view the variables and function information in the local scope.

1 A = 10 2 def func (): 3 A = 40 4 B = 20 5 def ABC (): 6 print ("Haha") 7 print (A, B) 8 print (globals () # view the global scope (built-in + global) content 9 print (locals () # view the local scope (internal function module objects, etc) content 10 11 func () 12 13 # globally available from a local location. however, it is not possible to find a local image from the global database.
1 # instance 2 def print (* ARGs): # declare function name print not built-in print3 pass4 # print (ARGs) # maximum recursion depth exceeded self-call recursive here the error 5 6 def func (): 7 print ("Heap") 8 9 func () # The function func is called with no return value, but the print in it is not built-in. Therefore, it is pass to find the global name "print" on the upper layer.

 

 







11th days-advanced functions

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.