Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of function Parameter call and non-fixed Parameter

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shares the content of an example analysis of function parameter calls and non-fixed parameters. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

def test(x,y,z): #this adds parameters

print(x)

print(y)

print(z)

test(1,2,3) #Add arguments here

1. Formal parameters and actual parameters 2. Position parameters and keywords

#test(1,2)#position parameter call: real participation parameter one-to-one correspondence, cannot be more, cannot be less

#test(y=1,x=2)#keyword: independent of parameter order

#test(y=1,x=2,1)#Keyword arguments cannot precede positional arguments

#test(1,z=2,y=5)

3. Default parameters: When calling a function, the default function can be optional. Usage: 1. Default installation value 2. Fixed default value

def test(x,y=2):

print(x)

print(y)

test(1,y=3)

Parameter group: parameter group should be placed at the end, such as test3 (name,x=2,*args)

#def test(*gg): #Accepts any number of arguments, in tuple form

print(gg)

test(1,2,3,4,5,5,5,5,5,7,9)

test(*[1,2,3,4,5]) # gg=tuple([1,2,3,4,5])

*args: Accepts n positional parameters, converts to tuples

def test(x,args): #sign represents function

print(x)

print(args)

#test(1,2,3,4,5,6,7)

#test([1,2,3,4,5,6,7]) #args=*[1,2,3,4,5,6,7]

Accepts n keyword parameters and converts them to dictionary form

def test1(**kwargs):!

print(kwargs)

print(kwargs["name"])

print(kwargs["age"])

test1(name="alex",age=8) #Convert n keyword parameters to dictionary

test1(**{"name":"alex","age":"8"})

Position parameters and keyword parameters

#def test3(name,**kwargs):

print(name)

print(kwargs)

test3("alex",age=18,sex="m")

Default parameters, location parameters, keyword parameters

def test3(name,x=2,**kwargs):

print(name)

print(x)

print(kwargs)

test3("alex",age=18,sex="m",x=4)

def test3(name,x=2,*args,**kwargs):

print(name)

print(x)

print(args)

print(kwargs)

test3("alex",age=18,sex="m",x=4)

Summary: positional parameters are passed only to formal parameters and *args, keyword parameters are passed to *kwargs!

Args will convert n positional parameters into the original form!

**kwargs converts n keyword arguments into dictionary form! m='alex'(m is key,'alex' is values)

Thank you for reading! About "function parameter call and non-fixed parameter example analysis" This article is shared here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge, if you think the article is good, you can share it to let more people see it!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report