Playing around with errors in Python - NameErrors
Let's start with NameErrors, which is one of the more common errors that a newcomer to Python will come across. It is reported when Python can't find a local or global variable in the code. One reason this might pop up is because a variable is being referred to outside of it's namespace. To give you an example
Let's walk through the code. After defining the variable a and the function test_f, you would naively expect the test_f() function call to change the value of a to 20 and print 20. You expect the print statement after the function call to also print 20. Because you expect the function call to have changed the value of a. But, if you try running the code for yourself, you'll notice that the final print statement will print 10. This is where namespaces come into the picture.
Now, let's try this instead
The call to the test_f function will set and print the variable b but the print statement afterwards will throw a NameError because outside of the test_f function, the variable b isn't defined.
Let's look at another example, this time in the context of classes in Python.
Let me explain the last statement first and then the rest of the example. test_c() creates an instance of the class test_c and test_c().test_f() calls the test_f method on the instance of the class. Naively, you would expect the code print 20, which is the value of the variable b in the class. But instead, you will get a NameError, telling you that the variable b isn't defined. The solution to this problem is to refer to b as self.b inside any of the methods defined on test_c, which tells Python that this variable belongs to the class on which the method is defined.
There are definitely a lot more ways in which you can make Python throw a NameError at you but I wanted to use the NameError to introduce the concept of namespaces in python. That's all for now. And as always, I am thankful for any feedback on the writing style and/or content. Until next time ...
[1]. hilite.me was used to create the inline code blocks.
[2]. You can refer to the Python official documentation on namespaces for more information.
[3]. A Python Shell can be accessed on the official Python page. A more comprehensive editor can be found here.
a = 10 def test_f(): a = 20 print a test_f() print a
Let's walk through the code. After defining the variable a and the function test_f, you would naively expect the test_f() function call to change the value of a to 20 and print 20. You expect the print statement after the function call to also print 20. Because you expect the function call to have changed the value of a. But, if you try running the code for yourself, you'll notice that the final print statement will print 10. This is where namespaces come into the picture.
Now, let's try this instead
def test_f(): b = 20 print b test_f() print b
The call to the test_f function will set and print the variable b but the print statement afterwards will throw a NameError because outside of the test_f function, the variable b isn't defined.
Let's look at another example, this time in the context of classes in Python.
class test_c: b = 20 def test_f(self): print b test_c().test_f()
Let me explain the last statement first and then the rest of the example. test_c() creates an instance of the class test_c and test_c().test_f() calls the test_f method on the instance of the class. Naively, you would expect the code print 20, which is the value of the variable b in the class. But instead, you will get a NameError, telling you that the variable b isn't defined. The solution to this problem is to refer to b as self.b inside any of the methods defined on test_c, which tells Python that this variable belongs to the class on which the method is defined.
There are definitely a lot more ways in which you can make Python throw a NameError at you but I wanted to use the NameError to introduce the concept of namespaces in python. That's all for now. And as always, I am thankful for any feedback on the writing style and/or content. Until next time ...
[1]. hilite.me was used to create the inline code blocks.
[2]. You can refer to the Python official documentation on namespaces for more information.
[3]. A Python Shell can be accessed on the official Python page. A more comprehensive editor can be found here.