Chapter 6. The Dynamic Typing Interlude
In the prior chapter, we began exploring Python’s core object types in depth with a look at Python numbers. We’ll resume our object type tour in the next chapter, but before we move on, it’s important that you get a handle on what may be the most fundamental idea in Python programming and is certainly the basis of much of both the conciseness and flexibility of the Python language—dynamic typing, and the polymorphism it yields.
As you’ll see here and later in this book, in Python, we do not declare the specific types of the objects our scripts use. In fact, programs should not even care about specific types; in exchange, they are naturally applicable in more contexts than we can sometimes even plan ahead for. Because dynamic typing is the root of this flexibility, let’s take a brief look at the model here.
The Case of the Missing Declaration Statements
If you have a background in compiled or statically typed
languages like C, C++, or Java, you might find yourself a bit
perplexed at this point in the book. So far, we’ve been using
variables without declaring their existence or their types, and it
somehow works. When we type a = 3
in an interactive session or program file, for instance, how does
Python know that a
should stand for
an integer? For that matter, how does Python know what a
is at all?
Once you start asking such questions, you’ve crossed over into the domain of Python’s dynamic typing model. In Python, types are determined automatically ...
Get Learning Python, 4th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.