Use function parameters

# Describes an argument between Form 
# parameter: parameter called the formal parameters defined in the definition phase function, referred to as parameter, corresponding to the variable name
# DEF FUNC (X, Y): X = #. 1, Y = 2
# Print (X, Y)

# arguments: the function call calling the actual parameter value passed in stages, referred to as the argument, equivalent to the value of the variable
# func (1,2)

the relationship between form argument #:
# 1, in the call phase, the argument (variable value) will be bound to the parameter (variable name)
# 2, this binding relationship can only be used in the body of the function
# 3, the binding relationship between actual participation in the formal parameter become effective when the function call after the end of the function call unbind

# argument value is passed, but the value may be in the form
# a form:
# FUNC (1,2)

# two forms:
# a. 1 =
# 2 = B
# FUNC ( a, B)

# three forms:
# FUNC (int ( '. 1'), 2)
# FUNC (func1 (1,2,), func2 (2,3), 333)

# dimorphic arguments involved in the specific use
# 2.1 positional parameters: sequentially from left to right according to the parameters defined position parameter called
# parameter position: in the function definition phase, from left to right in the order defined directly "variable name"
# features: must be delivered value, one more not less Nor a
# DEF FUNC (the X-, the y-):
# Print (the X-, the y-)
# FUNC (, 2, 3)
FUNC # (. 1,)

# position actual parameters: calling the function stage, from the left to the value passed in any order of
# features: the order of one-parameter

# FUNC (1,2)
# FUNC (2 , 1)

# 2.2 keyword parameter
# keyword argument: phase function call, incoming in the form of key = value value
# characteristics: by name to pass a parameter value, may refer to sequence completely
# DEF FUNC (X, Y):
# Print (X, Y)

# FUNC (Y = 2, X = 1)
# FUNC (1,2)

# mixture, emphasizes
# 1, must be placed in the position of the keyword argument argument front
# FUNC (. 1, Y = 2)
# FUNC (Y = 2,1)

# 2, could not be repeated for the same transmission parameter values
# FUNC (. 1, Y = 2, X =. 3)
# FUNC (. 1, 2,. 3 X =, Y =. 4)

# 2.3 default parameters
# default parameter: function definition phase, has been assigned a parameter, called the default parameters
# features: in the definition phase had been assigned, means in the call phase can not assign a value to
# DEF FUNC (the X-, the y-= 3):
# Print (the X-, the y-)
#
# # FUNC (= the X-1)
# FUNC (= the X-1, the y-= 44444)

# Def register (name, age, gender = ' M'):
# Print (name, Age, Gender)
#
# Register ( 'three guns', 18 is)
# Register (' Artillery ',. 19)
# Register (' cannon ', 19)
# the Register (' no gun ', 19,' female ')

# location-shaped participate default parameter mix, stressed:
# 1, position parameter must be in the default parameter on the left
# def func (y = 2, X):
# Pass

# 2, the default parameter values are assigned in the function definition phase, is specifically assigned memory address value
# model. 1:
# 2 = m
# DEF FUNC (X, Y = m) : # y => memory address 2
# Print (X, Y
# m = 3333333333333333333
# FUNC (. 1)

# model 2:
# m = [111111,]
#
# DEF FUNC (X, Y = m): # Y = > [111111] memory address
# Print (X, Y)
#
# m.append (3333333)
# FUNC (. 1)

#. 3, although default values may be specified as any data type, variable type but not recommended
# Ideal state function: calling function only has a relationship with the function itself, does not affect the external code
# m = [111111,]
#
# DEF FUNC (X, Y = m):
# Print (X, Y)
#
# m.append (3333333)
# m.append (444444)
# m.append (5555)
#
# FUNC (. 1)
# FUNC (2)
# FUNC (. 3)

# DEF FUNC (X, Y, Z, L = None) :
# IS IF L None:
# L = []
# l.append (X)
# l.append (Y)
# l.append (Z)
# Print (L)

# FUNC (l, 2,3)
# FUNC ( 4,5,6)

# new_l = [111,222]
# FUNC (l, 2,3, new_l)

# 2.4 variable length parameter (* and ** usage)
# variable length means when the function call, the number of incoming values (argument) is not fixed
# argument is used to the parameter assignment, the corresponding, actual parameters for the overflow must be received corresponding parameter

# 2.4.1 variable length positional parameters
The I #: * parameter name: overflow position for receiving arguments, the position of the overflow * real participants are saved as a tuple format then assigned immediately following parameter name
# * heel may be any name, However, convention should args

# DEF FUNC (X, Y, Z *): # Z = (3,4,5,6)
# Print (X, Y, Z)

# FUNC (1,2,3,4,5 ,. 6)

# DEF my_sum (* args):
# RES = 0
# args for in Item:
# = RES + Item
# return RES
#
# my_sum RES = (1,2,3,4,)
# Print (RES)

# II : * can be used in the arguments, the argument with *, the value of the first argument * broken into position
# DEF FUNC (X, Y, Z):
# Print (X, Y, Z)
#
# # FUNC (* [11,22,33]) # FUNC (11,22,33)
# # FUNC (* [11,22]) # FUNC (11,22)
#
# L = [11,22,33]
# FUNC (L *)

# III: Types of actual parameters are with *
# DEF FUNC (X, Y, * args): # args = (3,4,5,6)
# Print (X, Y, args)

FUNC # (1,2, [3,4,5,6])
# FUNC (1,2, * [3,4,5,6]) # FUNC (1,2,3,4,5,6)
FUNC # (* 'Hello') FUNC # ( 'H', 'E', 'L', 'L', 'O')

# 2.4.2 variable length key parameters
# I: ** parameter name : to receive an overflow of keyword arguments, ** keywords will overflow arguments saved into a dictionary format, and then assigned to closely followed by the parameter name
can be any name # ** heel, but the convention should is kwargs
# DEF FUNC (X, Y, kwargs **):
# Print (X, Y, kwargs)
#
# FUNC (. 1, Y = 2, A =. 1, B = 2, C =. 3)

# II: * * can be used in the argument (** heel only dictionary), with the arguments *, ** value of the first argument broken into Categories
# DEF FUNC (X, Y, Z):
# Print (X, Y, Z)

# FUNC (* { 'X':. 1, 'Y': 2, 'Z':. 3}) # FUNC ( 'X', 'Y', 'Z')
# FUNC ( {** 'X':. 1, 'Y': 2, 'Z':}. 3) FUNC # (X =. 1, Y = 2, Z =. 3)

# error
# func (** { 'x' : 1 , 'y': 2,} ) # func (x = 1,2 = Y)
# FUNC ({** 'X':. 1, 'A': 2, 'Z':}. 3) FUNC # (X =. 1, A = 2, Z =. 3)

# III: Types of actual both with reference **
DEF FUNC # (X, Y, kwargs **):
# Print (X, Y, kwargs)

# FUNC (Y = 222, X = 111, A = 333, B = 444)
# FUNC (** { 'Y' : 222, 'X': 111, 'A': 333, 'B': 4444})

# mix * and **: * args must kwargs before **
# DEF FUNC (X, * args, ** kwargs) :
# Print (args)
# Print (kwargs)
#
# FUNC (1,2,3,4,5,6,7,8, X =. 1, Y = 2, Z =. 3)

# DEF index (X, Y , Z):
# Print ( 'index = >>>', X, Y, Z)

# warpper DEF (* args, ** kwargs): args = # (. 1,) = {kwargs' Z ':. 3,' Y ': 2}
# index (* args, ** kwargs)
# index (* (. 1,), ** {' Z ':. 3,' Y ': 2})
# index (. 1,. 3 = Z, Y 2 =)

# wrapper (. 1,. 3 = Z, Y = 2) # parameter is passed to the wrapper with index
# --- original format "summary -----" prodding

Guess you like

Origin www.cnblogs.com/0B0S/p/12516734.html