One, custom function parameters
1. Type
(1) Position parameters
"X" is the positional parameter.
# !/usr/bin/env python # -*-coding:utf-8-*- # Author:dingkai # Mtime:2018/4/27 def Power (x): = x * x print(Result)
(2) Default parameters
"N" is the default parameter
# !/usr/bin/env python # -*-coding:utf-8-*- # Author:dingkai # Mtime:2018/4/27 def Power (x,n=1): = 1 while n > 0: = n-1 c17/>= S * x return s
(3) Variable parameters
"Numbers" is a mutable parameter, when the Calc function is called, the argument numbers is a list
def Calc (*numbers): = 0 for in numbers: = sum + N * n return sum
Print (Calc ([+]))
(4) Keyword parameters
"**every" is the keyword parameter
def person (name,age,**every):
Print (' name: ', Name, ' Age: ', age, ' other: ', every)
Extra = {' City ': ' Beijing ', ' job ': ' OPS '}
#person (' Dingkai ', 25,city = extra[' city '],job = extra[' job '])
#person (' Dingkai ', 25,**extra)
(5) named keyword parameter
"*" followed by the name of the keyword
def person (name,age,*,city, Job): print(name,age,city,job) #person (' Dinkai', 26,city= ' Beijing ', job='OPS')
2. Precautions:
(1) is a required parameter before, the default parameter is behind, otherwise the Python interpreter will error (think about why the default parameters can not be placed in front of the required parameters);
(2) is how to set default parameters.
python-Custom Functions-parameters