Python Introductory learning-day09-function basics and Parameters

Source: Internet
Author: User
Tags function definition wrapper

Function Basics Three forms of a defined function

1.1 No parameter function
def foo ():
Print (' from foo ')
Foo ()

1.2 Parameter function
def bar (x, y):
Print (x, y)
Bar (up to)

1.3 Null function
def func ():
Pass

def upload ():
Pass

def download ():
Pass

def login ():
Pass

def register ():
Pass

def ls ():
Pass

Three forms of the second call function


2.1 Statement Form
def foo ():
Print (' from foo ')
Foo ()

2.2 Form of an expression
def foo (x, y):
res = x + y
return res

Res=foo expression Form
Res1=foo (*100)
Print (RES1)

2.3 can be passed as a parameter to another function
def max2 (x, y):
If x > Y:
return x
Else
Return y

RES=MAX2 (MAX2), 3
Print (RES)

function return value The return value of a function needs to be noted:

1 return value no type limit
2 return value has no number limit
Returns 1 values: The call function gets the result of a value
Returns multiple values: The call function gets the result of a tuple
return 0 values, or do not write return: Call function to get the result is None

two return keyword:

Return is the function of the end of the flag, the function can have multiple return, but once executed, the entire function ends

Def f1 ():
Print (' first ')
Return 1
Print (' second ')
Return 2
Print (' third ')
Return 3

RES=F1 ()
Print (RES)

def foo ():
Count=1
While True:
Print (count)
if Count = = 3:
Return
Count+=1

Foo ()

def bar ():
Pass

def foo ():
return [1,2],1,1.3,{' x ': 1},bar

Res=foo ()
Print (RES)

def func ():
Print (' from foo ')
Return

Res=func ()
Print (RES)

The parameters of a function are divided into two main categories: 1 parameter: Refers to specifying the variable name in parentheses in the definition function, that is, the parameter essence is the "variable name" 2 argument: refers to the value passed in the parentheses of the calling function, that is, the argument essence is the "value"

The relation of the shape participates in the argument: when the function is called, the argument (value) is assigned (bound) to the parameter (the variable name),
This binding is temporarily in effect when the function is called and is invalidated after the call has ended.
def foo (x, y): #x =1 y=2
X=1
y=2
Print (x, y)

Foo (ON)

Specific classification of formal participation arguments
A positional parameter


1.1-bit parameter: a formal parameter defined sequentially from left to right in the defining function stage, called a positional parameter
Note: Any parameter defined by location must be passed, one more, and one less.
def foo (x, y):
Print (x, y)
Foo (#1), 2
Foo (#多), error
Foo (1,) #少, error

1.2-bit arguments: Values passed in from left to right in the calling function stage, called positional arguments
Note: Any arguments that are defined by location will correspond to the parameter one by one
def foo (x, y):
Print (x, y)

Foo (2,1)

Two keyword parameters

Keyword arguments: In the call function phase, the parameter value is named by the form of Key=value
Attention:
1. Can completely disrupt the order, but can still be named for the specified parameter value
2. You can mix the positional real participation keyword arguments, but be aware that:
2.1 The position argument must precede the keyword argument
2.2 cannot be assigned to a parameter repeatedly
def foo (name,age):
Print (Name,age)

Foo (' Egon ',)#name = ' Egon ' age=18
Foo (' Egon ')#name = ' age= ' Egon '
Foo (age=18,name= ' Egon ')#name = ' Egon ' age=18

Foo (' Egon ', age=18)#name = ' Egon ' age=18
Foo (name= ' Egon ')#报错
Foo (' Egon ', age=18,name= ' LXX ')#报错

Three default parameters

Default parameters: Refers to a parameter that has been assigned to a parameter at the defining function stage, which is called a formal parameter with a default value, which is referred to as the default formal parameter
Attention:
1. has been assigned during the definition phase, meaning that it is not possible to assign a value at the call stage
2. Position parameters should be placed before the default parameters
3. The value of the default parameter is fixed at the function definition stage.
4. The value of the default parameter should usually be an immutable type

def foo (x,y=2):
Print (x, y)

Foo (1)#x =1,y=2
Foo (1,3)#x =1,y=3
Foo (y=3,x=1)#x =1,y=3

m=10
def foo (x,y=m):#y =m=10
Print (x, y)

M=20
Foo (1) #x =1,y=10


def register (name,hobby,l=[]):
L.append (Hobby)
Print ('%s's hobby is%s '% (name,l))

Register (' Alex ', ' Piao ')
Register (' LXX ', ' Drink kidney soup ')
Register (' yxx ', ' no bathing ')
Register (' Egon ', ' read ')

Alex's hobby is [' Piao ']
LXX's hobby is [' Piao ', ' drink kidney soup ']
Yxx's hobby is [' Piao ', ' drink kidney soup ', ' Don't bathe ']
Egon's hobby is [' Piao ', ' drink kidney soup ', ' Don't bathe ', ' read ']

def register (name, hobby, L=none):
If L is None:
L=[]
L.append (Hobby)
Print ('%s's hobby is%s '% (name, L))

Register (' Alex ', ' Piao ')
Register (' LXX ', ' Drink kidney soup ')
Register (' yxx ', ' no bathing ')
Register (' Egon ', ' read ')

Alex's hobby is [' Piao ']
LXX's hobby is [' drink kidney soup ']
Yxx's hobby is [' Don't bathe ']
Egon's hobby for [' read ']

Positional parameter vs default parameter

For most cases, the values are different and should be defined as positional parameters
For most cases where the values are the same, they should be defined as default parameters

def register (name,age,sex= ' male '):
Print (Name,age,sex)

Register (' Li Tie Egg ', 18,)
Register (' Lee Eun-egg ', 28)
Register (' Copper egg ', 38)
Register (' Liu Braised Egg ', 48)
Register (' Ryuji ya ', 19, ' female ')

Li Tie Egg 18 male
Lee Eun-egg 28 male
Copper Egg 38 Male
Liu Braised Egg 48 male
Ryuji ya 19 Girls


Four variable-length parameters

At the angle of the argument, the variable length refers to the number of arguments passed in that are not fixed when the function is called
The arguments cannot be defined in two ways: positional arguments, key arguments, and there must be two solutions for the formal parameter * and * *, where the class should participate in the actual argument of the overflow position

1. In the formal parameter with *: Will call the function when the overflow location argument holds the form of the Narimoto group, and then assigns the value * after the variable name

def foo (x,y,*z): #z = (3,4,5,6)
Print (x, y, z)

Foo (1,2,3,4,5,6)

2. In the argument with *: Whenever in the argument with a star, before the value of the first break it into a positional argument, and then assign the value

def foo (x,y,*z): #z = (3,4,5,6)
Print (x, y, z)
Foo (1,*[2,3,4,5,6]) #foo (1,2,3,4,5,6)

def foo (x, Y, z):
Print (x, y, z)
Foo (1,* (2,3,4,5,6)) #foo (1,2,3,4,5,6)
Foo (* ()) #foo ( all in all)
Foo (* ' Hello ')# foo ()
Foo (* ' abc ') #foo (' A ', ' B ', ' C ')


3. In the formal parameter with * *: Will call the function when the Overflow keyword argument is saved to the form of a dictionary, and then assign the value * * after the variable name

def foo (x,y,**z): #z ={' z ': 3, ' a ': 1, ' B ': 2}
Print (x, y, z)

Foo (1,y=2,a=1,b=2,c=3)

4. In the argument with * *: Any in the actual argument with the star, before the value of the first break it into a keyword argument, and then to assign the value

def foo (x,y,**z):# z={' a ': +, ' B ': $
Print (x, y, z)

Foo (1,**{' a ': +, ' B ': $, ' Y ': 111})# foo (1,b=200,a=100,y=111)


def foo (x, Y, z):
Print (x, y, z)

Foo (**{' y ': 111, ' X ': 222, ' Z ': 333})# foo (z=333,x=222,y=111)


5. Specification: In formal parameters with * and *, the variable name should be args,** followed by the variable name should be Kwargs

def foo (*args,**kwargs): #args = (1,2,3,4,5) kwargs={' a ': 1, ' B ': 2, ' C ': 3}
Print (args)
Print (Kwargs)
Foo (1,2,3,4,5,a=1,b=2,c=3)


def bar (x, Y, z):
Print (x, y, z)

def wrapper (*args,**kwargs): #args = (1,2,3,4,5,6) kwargs={' a ': 1, ' B ': 2, ' C ': 3}
Bar (*args,**kwargs)
Bar (* (1,2,3,4,5,6), **{' a ': 1, ' B ': 2, ' C ': 3}) #bar (1,2,3,4,5,6,a=1,b=2,c=3)
Wrapper (1,2,3,4,5,6,a=1,b=2,c=3)

!!!!!!!!!!!!!!! When we want to pass the parameter format of a function to a function of its internal, it should use the following form
def bar (x, Y, z):
Print (x, y, z)

def wrapper (*args,**kwargs): #args = (up to) kwargs={' Z ': 3}
Bar (*args,**kwargs)
Bar (* (), **{' Z ': 3}) #bar (1,2,z=3)
Wrapper (1,2,z=3) #虽然调用的是wrapper, but to follow the parameter criteria that are indeed bar

Five-named keyword parameter: the argument between * and * is called a named keyword parameter


Note: The named keyword parameter must be passed as a Key=value value
def foo (X,y,*args,m,n,**kwargs): #args = (3,4,5,6,7,8)
Print (x, y)#
Print (args)# (3,4,5,6,7,8)
Print (m,n) #222, 333
Print (Kwargs)

Foo (1,2,3,4,5,6,7,8,n=333,m=222,a=1,b=2)


def foo (*,x=1,y):
Print (x)
Print (y)

Foo (y=2222,x=1111)

1111
2222

Foo (y=2222)

1
2222

Python Introductory learning-day09-function basics and Parameters

Related Article

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.