Dec-16-2020, 03:07 AM
This is from Reuven Lerner's book Python Workout.
I don't really understand why I don't get an error when no value is passed to y.
Or why, the second time around, I get the expected result.
(I added the print()s to show what is happening.
Why is there no error when y has no value??
Normally, if a function requires an argument and none is supplied, you will get an error.
Now I try:
I get the intended result:
How did 20 get passed to y??
I don't really understand why I don't get an error when no value is passed to y.
Or why, the second time around, I get the expected result.
(I added the print()s to show what is happening.
def foo(x): def bar(y): print(f'y is {y}') print(f'x is {x}') print(f'x * y is {x*y}') return x * y print(f'bar is {bar}') return barI call the function with g = foo(2), but y has no value. I expected an error, but I get:
Quote:g = foo(2) # returns: bar is <function foo.<locals>.bar at 0x7ff37b475268>
Why is there no error when y has no value??
Normally, if a function requires an argument and none is supplied, you will get an error.
Now I try:
print(g(20))but I don't get:
Quote:foo(20) # returns: bar is <function foo.<locals>.bar at 0x7ff37b475268>
I get the intended result:
Quote:y is 20
x is 2
x * y is 40
40
How did 20 get passed to y??