Python Basic trapping -03-function

Source: Internet
Author: User

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

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.