Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
The Python Apprentice

You're reading from   The Python Apprentice Introduction to the Python Programming Language

Arrow left icon
Product type Paperback
Published in Jun 2017
Publisher Packt
ISBN-13 9781788293181
Length 352 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Austin Bingham Austin Bingham
Author Profile Icon Austin Bingham
Austin Bingham
Robert Smallshire Robert Smallshire
Author Profile Icon Robert Smallshire
Robert Smallshire
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Getting started FREE CHAPTER 2. Strings and Collections 3. Modularity 4. Built-in types and the object model 5. Exploring Built-in Collection types 6. Exceptions 7. Comprehensions, iterables, and generators 8. Defining new types with classes 9. Files and Resource Management 10. Unit testing with the Python standard library 11. Debugging with PDB 12. Afterword – Just the Beginning
13. Virtual Environments 14. Packaging and Distribution 15. Installing Third-Party Packages

Imprudent return codes

Let's add a second function, string_log() to our module, which calls our convert() function and computes the natural log of the result:

from math import log

def string_log(s):
v = convert(s)
return log(v)

At this point we must confess that we've gone out of our way here to be deeply unPythonic by wrapping the perfectly good int() conversion, which raises exceptions on failure, in our convert() function which returns a good old-fashioned negative error code. Rest assured that this unforgivable Python heresy has been committed solely to demonstrate the greatest folly of error return codes: That they can be ignored by the caller, wreaking havoc amongst unsuspecting code later in the program. A slightly better program might test the value of v before proceeding to the log call.

Without such a check log() will of course fail when passed the negative...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at ₹800/month. Cancel anytime