Skip to Content
Hypermodern Python Tooling
book

Hypermodern Python Tooling

by Claudio Jolowicz
June 2024
Intermediate to advanced
270 pages
6h 40m
English
O'Reilly Media, Inc.
Content preview from Hypermodern Python Tooling

Chapter 10. Using Types for Safety and Inspection

What’s a type? As a first approximation, let’s say the type of a variable specifies the kind of values you can assign to it—​for example, integers or lists of strings. When Guido van Rossum created Python, most popular programming languages fell into two camps when it came to types: static and dynamic typing.

Statically typed languages, like C++, require you to declare the types of variables upfront (unless the compiler is smart enough to infer them automatically). In exchange, compilers ensure a variable only ever holds compatible values. That eliminates entire classes of bugs. It also enables optimizations: compilers know how much space the variable needs to store its values.

Dynamically typed languages break with this paradigm: they let you assign any value to any variable. Scripting languages like JavaScript and Perl even convert values implicitly—​say, from strings to numbers. This radically speeds up the process of writing code. It also gives you more leeway to shoot yourself into the foot.

Python is a dynamically typed language, yet it chose a middle ground between the opposing camps. Let’s demonstrate its approach with an example:

import math

number = input("Enter a number: ")
number = float(number)
result = math.sqrt(number)

print(f"The square root of {number} is {result}.")

In Python, a variable is just a name for a value. Variables don’t have types—values do. The program associates the same name, number, first with a ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Learning Python, 6th Edition

Learning Python, 6th Edition

Mark Lutz

Publisher Resources

ISBN: 9781098139575Errata Page