kwargs.py
This commit is contained in:
parent
a7986efee7
commit
70f0138aca
17
python-temel/kwargs.py
Normal file
17
python-temel/kwargs.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
def foo(a, b, **kwargs):
|
||||
legal_args = ['width', 'height', 'color', 'spec']
|
||||
for key in kwargs:
|
||||
if key not in legal_args:
|
||||
print('{} is invalid arguments'.format(key))
|
||||
return
|
||||
|
||||
width = kwargs.get('width', 1)
|
||||
height = kwargs.get('width', 1)
|
||||
color = kwargs.get('color', 'red')
|
||||
spec = kwargs.get('spec', 'default')
|
||||
|
||||
print(f'a = {a}, b = {b}, width = {width}, height = {height}, color = {color}, spec = {spec}')
|
||||
|
||||
foo(10, 20)
|
||||
foo(10, 20, color = 'blue', spec = 'x12')
|
||||
foo(10, 20, height = 10, color = 'red')
|
Loading…
Reference in New Issue
Block a user