Function: Typically a code module that is functionally divided
"1" built-in functions
Generally we use the module---can probably have an understanding of most of the usage is very simple
2 "function return value"
We should control each branch of the function. That is, the signal we get when the function is executed is the result of one of the branches.
Returns an object in Python with the return keyword
3 "Parameters"
Value-The parameter is the values
What we're talking about is the pass-through value.
The difference between the argument and the formal parameter. is the different values in different function spaces
Arguments we usually refer to the parameters of the global space
Formal parameter production refers to the parameters used in the function space.
Then Python adds several additional formal parameter representations to the function so that we could use
That's the default parameter and the dynamic parameter.
Default parameters:
Import datetime as dt def log_time (message, Time=none): If Time is None: time=dt.datetime.now () print ("{0}: {1} ". Format (Time.isoformat (), message))
That is, when the function is called, the parameter is not passed, so the default value is assigned to the parameter.
--------------------------------------------------------------------------------------------------------------- --------------
Dynamic Parameters:
Just copy it, lazy lie.
Class Person: def __init__ (self,*pros,**attrs): self.name = "Jeff" Self.pros = Pros for (Key,value) in Attrs.items (): stm = "self.%s =/"%s/""% (key,value) exec (STM) if __name__ = = "__main__": Jeff = Person (1,2,3,sex= "Boy") Print Jeff.pros print jeff.sex print dir (Jeff)
The printed content is
(1, 2, 3)
Boy
[' __doc__ ', ' __init__ ', ' __module__ ', ' name ', ' Pros ', ' sex ']
Python Indeterminate parameters:
A parameter that begins with an * number represents an array of any length that can receive successive strings of arguments, as in the code above, you can do more.
The argument begins with a two * number that represents a dictionary with the form "Key=value", which accepts any number of consecutive arguments.
Within the function, we can treat the former as a tuple, and the printed result is a tuple. Note that the calling method is a variable length parameter, and when the method executes, it is set, so it is a tuple. At the same time, we can treat the latter as a dictionary.
Python Basic trapping -03-function