Chapter 6. The Dynamic Typing Interlude
In the prior chapter, we began exploring Pythonâs core object types in depth by studying Python numeric types and operations. 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 implies.
As youâll see here and throughout this book, in Python, we do not declare the specific types of the objects our scripts use. In fact, most 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, and is also a potential stumbling block for newcomers, letâs take a brief side trip to explore 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 ...
Get Learning Python, 5th 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.