6.11. The Dynamic Nature of Python

Python is what is known as a dynamically typed language. That is, its variables are not restricted to referencing objects of any particular type by the language. In Python, variables spring into existence as required. They do not need to be declared beforehand. Moreover, a variable x can be an integer one minute, a string the next, and an user-defined object the next. This is a very flexible way of doing things and can be a great boon for certain types of programming tasks. However, it does mean you have to be careful, as the example below will illustrates.

CD-ROM reference=6042.txt
>>> def DaysFromHours(h):
					...return h / 24
...
>>> DaysFromHours(240)
10
>>> DaysFromHours("Hello World") Traceback (innermost ...

Get XML Processing with Python 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.