0

I want to default the parameter C to 20 if I don't supply any values.

How is this possible ?

functypes(1,2,,4,5,6,id='jijo',job='engineer')

I have tried this but it shows syntax error :

functypes(1,2,,4,5,6,)
                  ^
SyntaxError: invalid syntax

def functypes(a,b,c=20,*y,**Z):
    print("the values of a,b,c are",a,b,c)
    for y1 in y:
        print("The values in Y are",y1,",",end='')
    for z1,z2 in Z.items():
        print("The values in Z are",z1,z2)

The following call works fine :

functypes(1,2,,4,5,6,id='jijo',job='engineer')

4 Answers 4

2

change def functypes(a,b,c=20,*y,**Z): to def functypes(a,b,*y,c=20,**Z): this will mintain c and keep values as desired , since c is key-value pair need to add vefore key value

def functypes(a,b,*y,c=20,**Z):
    print("the values of a,b,c are",a,b,c)
    for y1 in y:
        print("The values in Y are",y1,",",end='\n')
    for z1,z2 in Z.items():
        print("The values in Z are",z1,z2)

functypes(1,2,3,4,5,6,id='jijo',job='engineer')

""" output 
the values of a,b,c are 1 2 20
The values in Y are 3 ,
The values in Y are 4 ,
The values in Y are 5 ,
The values in Y are 6 ,
The values in Z are id jijo
The values in Z are job engineer
"""
Sign up to request clarification or add additional context in comments.

Comments

2

Putting the default arg after *args makes it a keyword-only argument which can be only specified by name, not by position.

def functypes(a,b,*y,c=20,**Z):
    print("the values of a,b,c are",a,b,c)
    for y1 in y:
        print("The values in Y are",y1,",",end='')
    for z1,z2 in Z.items():
        print("The values in Z are",z1,z2)

functypes(1,4,5,6,7,8,c=9,id='jijo',job='engineer') #take c as 9
functypes(1,4,5,6,7,8,id='jijo',job='engineer') #take c as 20

Comments

1

You cannot. Python does not allow to pass an explicit empty parameter. Said differently you are not allowed to write consecutive commas (,,) in a function call, so you can only pass keyword arguments after an empty parameter

A common idiom is to use a None value:

def functypes(a,b,c=None,*y,**Z):
    if c is None: c=20
    print("the values of a,b,c are",a,b,c)
    for y1 in y:
        print("The values in Y are",y1,",",end='')
    for z1,z2 in Z.items():
        print("The values in Z are",z1,z2)

You can then use: functypes(1,2,None,4,5,6,id='jijo',job='engineer')


Alternatively you can pass explicitely the y argument as a keyword: functypes(1,2,y=[4,5,6],id='jijo',job='engineer') but it really looks like an anti-pattern...

Comments

0

Somebody posted and This worked.

functypes(1,2,*[3,4,5,6],id='jijo',job='engineer')

1 Comment

functypes(1,2,*[3,4,5,6],id='jijo',job='engineer') it is same as functypes(1,2,3,4,5,6,id='jijo',job='engineer') just here you have unpacked list given in abouve you have a list that cintain all dat a and uit is unpacked insidde function call

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.