fopp
fopp copied to clipboard
Variables and Parameters subchapter could use a couple of CYU quesitons
I'm also thinking that the following example would be good to add to this section:
>>> v1 += 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'v1' is not defined
>>> def foo():
... v1 += 1
...
>>> foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in foo
UnboundLocalError: local variable 'v1' referenced before assignment
Notice the different error messages! Subtle, but it really illustrates an important local variable concept.
Local variables are created at the same time the local namespace is created. That is ANY variable that is assigned to anywhere in the function gets added to the local namespace immediately but it will remain UNBOUND until the assignment statement is executed.
@presnick @laumurph what do you think?
I think that would be a good thing to add. It might also encourage students to pay more attention to error messages if they're not already doing that.