-1

Let's consider foo(arg1=123, arg2=None) and foo(arg1=123).
Tell me please, if these two ways are equivalent ?

newbie
  • 597
  • 2
  • 6
  • 11
  • 3
    Hint: *Why* do you think they are equivalent? – heemayl Feb 26 '18 at 16:51
  • Do you want [overloaded](https://stackoverflow.com/a/30693266/1015062) function? – GarfieldCat Feb 26 '18 at 17:00
  • @heemayl because in case of `foo(arg1=123)` we have also that `arg2=None`. Yeah ? – newbie Feb 26 '18 at 17:02
  • I've added an answer. – heemayl Feb 26 '18 at 17:09
  • @newbie Only if `def foo(arg1, arg2=None)`. There is no implicit default value; if `foo` is defined as `def foo(arg1, arg2)`, then `foo(arg1=123)` is an error, because you didn't provide a value for the required argument `arg2`. – chepner Feb 26 '18 at 17:32
  • If those are definitions, then they are clearly not equivalent. If those are *calls*, then whether they are equivalent depends on how `foo` was defined. – chepner Feb 26 '18 at 17:34

1 Answers1

1

No, the two given function signatures (and hence functions) are not equivalent.

In foo(arg1=123, arg2=None), you have two arguments -- arg1 and arg2, which can be used inside the function as local names. Note that, assigning a value of None to some name does not make it anything special/different as far as the assignment statements are concerned. It is in fact a common way to give a placeholder value for a variable that is not mandatory or may be an empty mutable object.

On the other hand, foo(arg1=123) has only one argument arg1, which is available on the function's local scope for use.


Edit:

If you have a function defined as foo(arg1, arg2), both arguments are mandatory (positional) arguments.

So, foo(arg1=21) will throw a TypeError as you have not provided arg2. Whereas, foo(arg1=21, arg2=None) will work just fine as you have provided values for both the arguments.


Edit2:

If you have a function defined as foo(arg1=None, arg2=None) (or something similar i.e. with default values), both arguments are optional (keyword) arguments. In that case, both of the mentioned definitions would be the same.

heemayl
  • 39,294
  • 7
  • 70
  • 76
  • I meant something differ. We have function: `foo(arg1, arg2)`. Now, the question is: Are there equivalent two **calls**: `foo(arg1=21)` and `foo(arg1=21, arg2=None)`. – newbie Feb 26 '18 at 17:19
  • thanks for your involvment. Can you give some another example when these two calls will be equivalent ? – newbie Feb 26 '18 at 17:25
  • @newbie These two calls will never be equivalent. Now, i'm not sure what are you getting at. – heemayl Feb 26 '18 at 17:26
  • You used term: `mandatory (positional) `. What about situation when these arguments are not mandatory ? – newbie Feb 26 '18 at 17:32