Meet Lalwani
Meet Lalwani

Reputation: 3

why does the default argument change?

why does the default argument change in this case shouldn't it be the same always even when I try to change it

def myfunc(x,y,z="bye"):
    print("hey :" ,x)
    print("hey :" ,y)
    print("hey :" ,z)
myfunc("meet","bye","yo")

the default argument changes

Upvotes: 0

Views: 26

Answers (1)

Nastor
Nastor

Reputation: 638

Default argument gets overwritten if you explicitly specify its value when you call the function. It applies only if you call the function without passing the variable that holds the default value.

Have a look at this example:

>>> def myfunc(x,y,z="bye"):
...     print("hey :" ,x)
...     print("hey :" ,y)
...     print("hey :" ,z)
...
>>> myfunc("meet","bye","yo")
hey : meet
hey : bye
hey : yo
>>> myfunc("meet","bye")
hey : meet
hey : bye
hey : bye

Upvotes: 1

Related Questions