You are previewing Python 3 Object Oriented Programming.
O'Reilly logo
Python 3 Object Oriented Programming

Book Description

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.

  • Learn how to do Object Oriented Programming in Python using this step-by-step tutorial

  • Design public interfaces using abstraction, encapsulation, and information hiding

  • Turn your designs into working software by studying the Python syntax

  • Raise, handle, define, and manipulate exceptions using special error objects

  • Implement Object Oriented Programming in Python using practical examples

  • In Detail

    Object Oriented Programming is a very important aspect of modern programming languages. The basic principles of Object Oriented Programming are relatively easy to learn. Putting them together into working designs can be challenging.

    This book makes programming more of a pleasure than a chore using powerful Python 3 object-oriented features of Python 3. It clearly demonstrates the core OOP principles and how to correctly implement OOP in Python.

    Object Oriented Programming ranks high in importance among the many models Python supports. Yet, many programmers never bother learning the powerful features that make this language object oriented.

    The book teaches when and how OOP should be correctly applied. It emphasizes not only the simple syntax of OOP in Python, but also how to combine these objects into well-designed software.

    This book will introduce you to the terminology of the object-oriented paradigm, focusing on object-oriented design with step-by-step examples. It will take you from simple inheritance, one of the most useful tools in the object-oriented programmer's toolbox, all the way through to cooperative inheritance, one of the most complicated. You will be able to raise, handle, define, and manipulate exceptions.

    You will be able to integrate the object-oriented and the not-so-object-oriented aspects of Python. You will also be able to create maintainable applications by studying higher level design patterns. You'll learn the complexities of string and file manipulation, and how Python distinguishes between binary and textual data. Not one, but two very powerful automated testing systems will be introduced to you. You'll understand the joy of unit testing and just how easy they are to create. You'll even study higher level libraries such as database connectors and GUI toolkits and how they apply object-oriented principles.

    Harness the power of Python 3 objects; it's not just a scripting language

    Table of Contents

    1. Python 3 Object Oriented Programming
      1. Python 3 Object Oriented Programming
      2. Credits
      3. About the Author
      4. About the Reviewers
      5. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Errata
          2. Piracy
          3. Questions
      6. 1. Object-oriented Design
        1. Object-oriented?
        2. Objects and classes
        3. Specifying attributes and behaviors
          1. Data describes objects
          2. Behaviors are actions
        4. Hiding details and creating the public interface
        5. Composition and inheritance
          1. Inheritance
            1. Inheritance provides abstraction
            2. Multiple inheritance
        6. Case study
        7. Exercises
        8. Summary
      7. 2. Objects in Python
        1. Creating Python classes
          1. Adding attributes
          2. Making it do something
          3. Initializing the object
          4. Explaining yourself
        2. Modules and packages
          1. Organizing the modules
            1. Absolute imports
            2. Relative imports
        3. Who can access my data?
        4. Case study
        5. Exercises
        6. Summary
      8. 3. When Objects are Alike
        1. Basic inheritance
          1. Extending built-ins
          2. Overriding and super
        2. Multiple inheritance
          1. The diamond problem
          2. Different sets of arguments
        3. Polymorphism
        4. Case study
        5. Exercises
        6. Summary
      9. 4. Expecting the Unexpected
        1. Raising exceptions
          1. Raising an exception
          2. What happens when an exception occurs?
        2. Handling exceptions
        3. Exception hierarchy
          1. Defining our own exceptions
        4. Exceptions aren't exceptional
        5. Case study
        6. Exercises
        7. Summary
      10. 5. When to Use Object-oriented Programming
        1. Treat objects as objects
        2. Using properties to add behavior to class data
          1. How it works
          2. Decorators: another way to create properties
          3. When should we use properties?
        3. Managing objects
          1. Removing duplicate code
          2. In practice
          3. Or we can use composition
        4. Case study
        5. Exercises
        6. Summary
      11. 6. Python Data Structures
        1. Empty objects
        2. Tuples and named tuples
          1. Named tuples
        3. Dictionaries
          1. When should we use dictionaries?
          2. Using defaultdict
        4. Lists
          1. Sorting lists
        5. Sets
        6. Extending built-ins
        7. Case study
        8. Exercises
        9. Summary
      12. 7. Python Object-oriented Shortcuts
        1. Python built-in functions
          1. Len
          2. Reversed
          3. Enumerate
          4. Zip
          5. Other functions
        2. Comprehensions
          1. List comprehensions
          2. Set and dictionary comprehensions
          3. Generator expressions
        3. Generators
        4. An alternative to method overloading
          1. Default arguments
          2. Variable argument lists
          3. Unpacking arguments
        5. Functions are objects too
          1. Using functions as attributes
          2. Callable objects
        6. Case study
        7. Exercises
        8. Summary
      13. 8. Python Design Patterns I
        1. Design patterns
        2. Decorator pattern
          1. Decorator example
          2. Decorators in Python
        3. Observer pattern
          1. Observer example
        4. Strategy pattern
          1. Strategy example
          2. Strategy in Python
        5. State pattern
          1. State example
          2. State versus strategy
        6. Singleton pattern
          1. Singleton implementation
          2. Module variables can mimic singletons
        7. Template pattern
          1. Template example
        8. Exercises
        9. Summary
      14. 9. Python Design Patterns II
        1. Adapter pattern
        2. Facade pattern
        3. Flyweight pattern
        4. Command pattern
        5. Abstract factory pattern
        6. Composite pattern
        7. Exercises
        8. Summary
      15. 10. Files and Strings
        1. Strings
          1. String manipulation
          2. String formatting
            1. Escaping braces
            2. Keyword arguments
            3. Container lookups
            4. Object lookups
            5. Making it look right
          3. Strings are Unicode
            1. Converting bytes to text
            2. Converting text to bytes
          4. Mutable byte strings
        2. File IO
          1. Placing it in context
          2. Faking files
        3. Storing objects
          1. Customizing pickles
          2. Serializing web objects
        4. Exercises
        5. Summary
      16. 11. Testing Object-oriented Programs
        1. Why test?
          1. Test-driven development
        2. Unit testing
          1. Assertion methods
            1. Additional assertion methods in Python 3.1
          2. Reducing boilerplate and cleaning up
          3. Organizing and running tests
          4. Ignoring broken tests
        3. Testing with py.test
          1. One way to do setup and cleanup
          2. A completely different way to set up variables
          3. Test skipping with py.test
          4. py.test extras
        4. How much testing is enough?
        5. Case Study
          1. Implementing it
        6. Exercises
        7. Summary
      17. 12. Common Python 3 Libraries
        1. Database access
          1. Introducing SQLAlchemy
            1. Adding and querying objects
            2. SQL Expression Language
        2. Pretty user interfaces
          1. TkInter
          2. PyQt
          3. Choosing a GUI toolkit
        3. XML
          1. ElementTree
            1. Constructing XML documents
          2. lxml
        4. CherryPy
            1. A full web stack?
              1. Jinja Templating
              2. The CherryPy blog web application
        5. Exercises
        6. Summary