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
Python 3 Object Oriented Programming

You're reading from   Python 3 Object Oriented Programming If you feel it’s time you learned object-oriented programming techniques, this is the perfect book for you. Clearly written with practical exercises, it’s the painless way to learn how to harness the power of OOP in Python.

Arrow left icon
Product type Paperback
Published in Jul 2010
Publisher Packt
ISBN-13 9781849511261
Length 404 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Dusty Phillips Dusty Phillips
Author Profile Icon Dusty Phillips
Dusty Phillips
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Python 3 Object Oriented Programming
Credits
About the Author
About the Reviewers
Preface
1. Object-oriented Design FREE CHAPTER 2. Objects in Python 3. When Objects are Alike 4. Expecting the Unexpected 5. When to Use Object-oriented Programming 6. Python Data Structures 7. Python Object-oriented Shortcuts 8. Python Design Patterns I 9. Python Design Patterns II 10. Files and Strings 11. Testing Object-oriented Programs 12. Common Python 3 Libraries Index

Comprehensions


We've already seen a lot of Python's for loop. It allows us to loop over any object that supports the iterable protocol and do something specific with each of the elements in turn.

Supporting the iterable protocol simply means an object has an __iter__ method that returns another object that supports the iterator protocol. Supporting the iterator protocol is a fancy way of saying it has a __next__ method that either returns the next object in the sequence, or raises a StopIteration exception when all objects have been returned.

As you can see, the for statement, in spite of not looking terribly object-oriented, is actually a shortcut to some extremely object-oriented designs. Keep this in mind as we discuss comprehensions, as they, too, appear to be the polar opposite of an object-oriented tool. Yet, they use the same iteration protocol as for loops. They're just another kind of shortcut.

List comprehensions

List comprehensions are one of the most powerful tools in Python, so...

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 $19.99/month. Cancel anytime