-1

Let's say this is the signature of my function:

def foo(x,y,z=0):
.
.

When I want to use this function, how can I override the value of z without changing the function or the signature?

David
  • 733
  • 2
  • 11
  • 30
  • 1
    What do you mean override? – Andrew Li Aug 21 '16 at 13:54
  • 1
    Why would you change z's default value? The reason you change it in the first place is to be default. – Andrew Li Aug 21 '16 at 13:55
  • Do you mean how to pass in a value for `z` when calling the function? – Moon Cheesez Aug 21 '16 at 13:56
  • `def bar(x,y,z=5): return foo(x,y,z)`? – Aran-Fey Aug 21 '16 at 13:56
  • You can't. You could assign a *new* function object to the name `foo` with a different default that calls the old function object. Would that do? – Martijn Pieters Aug 21 '16 at 13:56
  • Can't you simply pass the values to the function `foo(2,3,4)` ? Not used to python. Try this out though. In this case, `z would be assigned 4` . – Mathews Mathai Aug 21 '16 at 13:57
  • Someone else wrote this function, and he defined it, now I have to use this function but with another value of z. – David Aug 21 '16 at 13:58
  • @ Moon Cheesez, yes. – David Aug 21 '16 at 13:59
  • You could keep calling the function as the need arises while passing different values as arguments. First call- `foo(2,3,4)` and second time may be `foo(2,3,8)`.....and so on. You can call a function more than once and pass different values for the parameters every time. There's no restriction for the values of arguments you pass neither for the number of calls. – Mathews Mathai Aug 21 '16 at 14:00

1 Answers1

5

Just pass value explicitly.

def foo(x, y, z=0):
    print z

foo(1,3)
>> 0 # default value

foo(1,2,5)
>> 5 # new value passed
Nidhin Bose J.
  • 1,092
  • 15
  • 28