Understanding the problem in more detail
Hopefully, it will not be surprising that this code causes a (different) TypeError
:
>>> example = 1
>>> example = "not an integer"
>>> example + 1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
A name can only refer to one thing at a time; when we start using it for something else, it's no longer used for the first thing. When the addition is attempted, there is not an ambiguity between example
meaning the integer or the string: it definitely means the string. (Of course, we can cause other kinds of errors by reusing a name, too. However, my guess is that TypeError
is the most common kind of error here. After all - if we reused the name for something of the same type, isn't that more likely to be intentional?)
What's sometimes less obvious - especially for programmers who were taught with traditional terminology like "variable", rather than Python-appropriate terminology like "name" - is that there are other ways to assign a name, besides the assignment operator.
In Python, everything is an object, and we take that seriously. Unlike in, say, Java, Python's functions, classes, and modules are objects - which means, among other things, that they have the same kind of names that integers and strings do.
Writing def example(): pass
is creating an object that is a function (it doesn't merely represent the function for use in reflection; it really is already, itself, an object), and assigns it to the name example
. Which means, if there was something else - say, a list - with the same name in the same scope, that other object no longer has that name. It's been reused. Functions don't get a separate scope or a separate namespace. They have the same kind of names as everything else; they aren't special. (Remember, Python figures out types at runtime, and "function" is just another type.)
So, if we create a list and then try to create a function with the same name, we can't index into the list with that name - because the name is used for the function, not the list any more. Similarly the other way around: if we write the function (or the function already exists, because it's a builtin) and then try to use its name for something else, we can't call the original function any more. If we used the name for something that isn't callable, we can't use that name to call anything.
Here are some other examples of ways to assign names, with thanks to https://nedbatchelder.com/text/names1.html. In each case, example
is the name that gets assigned.
Using the name for iteration in a for
loop or a comprehension or generator expression (for example in data:
, [f(example) for example in data]
etc. In a generator expression, of course, the name is only assigned when an element is requested from the generator)
Calling a function (supposing we have def func(example): pass
, then func(1)
does the assignment)
Making a class (class Example:
- hopefully this is not surprising, since we already noted that classes are objects)
Importing a module (import example
), or a module's contents (from my.package import example
) etc. (Hopefully this is not surprising, since we already noted that modules are objects. Incidentally, packages are objects too, but Python represents them with the same module
type)
The as
clause when handling an exception (except Exception as example:
) or using a context manager (with open('example.txt') as example:
)