Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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
Robert Smallshire Robert Smallshire
Author Profile Icon Robert Smallshire
Robert Smallshire
Austin Bingham Austin Bingham
Author Profile Icon Austin Bingham
Austin Bingham
Arrow right icon
View More author details
Toc

Table of Contents (21) Chapters Close

Title Page
Credits
About the Authors
www.PacktPub.com
Customer Feedback
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

Exceptions and control flow


Since exceptions are a means of control flow, they can be clumsy to demonstrate at the REPL, so in this chapter we'll be using a Python module to contain our code. Let's start with a very simple module we can use for exploring these important concepts and behaviors. Place the this code in a module called exceptional.py:

"""A module for demonstrating exceptions."""

def convert(s):
    """Convert to an integer."""
    x = int(s)
    return x

Import the convert() function from this module into the Python REPL:

$ python3
Python 3.5.1 (v3.5.1:37a07cee5969, Dec  5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from exceptional import convert

and call our function with a string to see that it has the desired effect:

>>> convert("33")
33

If we call our function with an object that can't be converted to an integer, we get a traceback from the int() call:

>&gt...
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 £13.99/month. Cancel anytime
Visually different images