Book Image

The Python Apprentice

By : Robert Smallshire, Austin Bingham
Book Image

The Python Apprentice

By: Robert Smallshire, Austin Bingham

Overview of this book

Experienced programmers want to know how to enhance their craft and we want to help them start as apprentices with Python. We know that before mastering Python you need to learn the culture and the tools to become a productive member of any Python project. Our goal with this book is to give you a practical and thorough introduction to Python programming, providing you with the insight and technical craftsmanship you need to be a productive member of any Python project. Python is a big language, and it’s not our intention with this book to cover everything there is to know. We just want to make sure that you, as the developer, know the tools, basic idioms and of course the ins and outs of the language, the standard library and other modules to be able to jump into most projects.
Table of Contents (21 chapters)
Title Page
Credits
About the Authors
www.PacktPub.com
Customer Feedback
Preface
12
Afterword – Just the Beginning

Collection protocols


In Python, a protocol is a group of operations or methods that a type must support if it is to implement that protocol. Protocols needn't be defined in the source code as separate interfaces or base classes as they would in a nominally typed language such as C# or Java. It's sufficient to simply have an object provide functioning implementations of those operations.

We can organize the different collections we have encountered in Python according to which protocols they support:

Protocol

Implementing collections

Container

str, list, dict, range, tuple, set, bytes

Sized

str, list, dict, range, tuple, set, bytes

Iterable

str, list, dict, range, tuple, set, bytes

Sequence

str, list, tuple, range, bytes

Mutable Sequence

list

Mutable Set

set

Mutable Mapping

dict

Support for a protocol demands specific behavior from a type.

Container protocol

The container protocol requires that membership testing using the in and not in operators be supported:

item in container
item not in container

Sized protocol...