Nik - 1 year ago 52

Python Question

Here is the working code:

`def g(y=10):`

return y**2

def f(x,y=10):

return x*g(y)

print(f(5)) #->500

However, let's suppose we don't want to remember and copy a default value of keyword parameter

`y`

`g`

One way to do that:

`def f(x,y=None):`

if y==None: return x*g()

else: return x*g(y)

But is there a cleaner way to do the same?

Something like:

`def f(x,y=empty()):`

return x*g(y)

Answer Source

Interesting question! Here's another possibility, however this requires handing in the second parameter as a named parameter.

```
>>> def g(y=10):
... return y**2
...
>>> def f(x, **kwargs):
... return x * g(**kwargs)
...
>>> f(5)
500
>>> f(5, y=0)
0
```