به نام خداوند مهربان

۱ مطلب در مهر ۱۴۰۲ ثبت شده است

استفاده ستاره در پارامتر های ارسالی:

 

def foo(a, b, c):
    return(a, b, c)

t = (1, 4, 6)
print('Output tuple unpacking: ',foo(*t))

d = {'a': 10, 'b': 20, 'c': 30}
print('Output dictionary unpacking: ',foo(*d))
print('Output dictionary unpacking: ',foo(**d))


 

خروجی:

    
Output tuple unpacking:  (1, 4, 6)
Output dictionary unpacking:  ('a', 'b', 'c')
Output dictionary unpacking:  (10, 20, 30)

استفاده در آرگمان:

def test_var_args(farg, *args):
    print ("formal arg:", farg)
    for arg in args:
        print ("another arg:", arg)

test_var_args(1, "two", 3)


def test_var_kwargs(farg, **kwargs):
    print ("formal arg:", farg)
    for key in kwargs:
        print ("another keyword arg: %s: %s" % (key, kwargs[key]))

test_var_kwargs(farg=1, myarg2="two", myarg3=3)

خروجی:

 

formal arg: 1
another arg: two
another arg: 3
formal arg: 1
another keyword arg: myarg2: two
another keyword arg: myarg3: 3

 

 

 

 

 

 

  • حسن دلدار