Python Road "third": Python Basics (17)--Function Dynamic parameters

Source: Internet
Author: User

#动态参数: * * 2 star default will pass in the parameters, all placed in the dictionary F1 (**{"KL": "V1", "K2": "V2"})
#带2星的用来处理那些带有键值对的值, that is, the value of a key one value

# example
# def func (**kwargs):
# Print (Kwargs,type (Kwargs))
# # Execution Mode one
# func (name= ' Wupeiqi ', age=18)
# # Execution Mode two
# li = {' name ': ' Wupeiqi ', ' age ':, ' gender ': ' Male '}
# func (**li)
# # Output
# # {' Age ': ' Name ': ' Wupeiqi '} <class ' Dict ' >
# {' Age ': ' Gender ': ' Male ', ' name ': ' Wupeiqi '} <class ' Dict ' >

# # exercise 1
# def F1 (**args): #两星 type is a dictionary, one element of the dictionary is a key-value pair Key-value, which must be passed with the specified parameters.
# Print (Args,type (args))

# f1 ("Hello") #两星的情况下, passing a parameter will cause an error.
# Output Error:
‘‘‘
Traceback (most recent):
File "c:/users/jam/pycharmprojects/s13/d3v1/12_ function 8_ dynamic parameter _2 star is a dictionary. Py", line one, in <module>
F1 ("Hello") #
TYPEERROR:F1 () takes 0 positional arguments but 1 was given
‘‘‘

# # Exercise 2
# def F1 (**args): #
# Print (Args,type (args))
# f1 (n1= "Hello") #这相当于一个指定参数, as a key value pair for the dictionary, take N1 as key and "Hello" as value
# output
# {' N1 ': ' Hello '} <class ' Dict ' >

# # Exercise 3
# def F1 (**args): #
# Print (Args,type (args))
# f1 (n1= "Hello", n2=18) #作为字典的两个键值对, N1 is key,n2 is key
# output
# {' N2 ': ' N1 ': ' Hello '} <class ' Dict ' >

# # Exercise 3
# def F1 (**args): #
# Print (Args,type (args))
# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}
# f1 (kk=dic) #kk当做一个key, dictionary dic as a value
# output
# {' KK ': {' K2 ': ' v2 ', ' K1 ': ' v1 '}} <class ' Dict ' >

# # 4 Note: If the formal parameter has 2 stars, the actual parameter also has a 2-star case, which is equivalent to the direct assignment, and what value of DIC is the value of args.
# def F1 (**args): #
# Print (Args,type (args))
# dic = {' K1 ': ' v1 ', ' K2 ': ' V2 '}
# f1 (**dic) #实际参数有2星
#
# # # Output
# # # {' K1 ': ' v1 ', ' K2 ': ' v2 '} <class ' Dict ' >

Python Road "third": Python Basics (17)--Function Dynamic parameters

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.