Sessions at PyCon US 2012 about Python and Object Oriented Programming on Friday 9th March

Your current filters are…

Clear
  • Introduction to Metaclasses

    by Luke Sneeringer

    Python's metaclasses grant the Python OOP ecosystem all the power of more complex object inheritance systems in other languages, while retaining for most uses the simplicity of the straightforward class structures most developers learn when being introduced to object-oriented programming. This talk is an explanation of metaclasses: first, what they are, and second, how to use them.

    • Metaclasses
    • Introduction (2.5m)
    • Python's metaclasses grant the Python OOP ecosystem all the power of more complex object inheritance systems in other languages, while retaining the simplicity of the straightforward class structure that traditional C++ and Java programmers learned, and is taught in programming courses.
    • Classes are Objects, Too! (5m)
    • Classes are first-class objects in Python, like functions/methods
    • Classes, like other objects, can be assigned to variables and passed as arguments
    • ...and this ability is one of the tricks in the reusable code toolbox
    • Concept: Metaclasses generate classes. (5m)
    • The hierarchy starts with "type"
    • Classes are themselves instances of their metaclasses
    • By extension, classes provide code that runs when instances are created, while metaclasses provide code that runs when classes are created.
    • Remember the "analogies" section on standardized tests in the United States (and many other countries)?
    • Babylon 5 : J. Michael Strazynski :: Star Trek : ___
    • Instances : Classes :: Classes : Metaclasses
    • Think about a self-enclosed machine that creates, say, t-shirts. The machine is the class; the individual shirts are the instances. The guy who builds the t-shirt machines is the metaclass.
    • Concrete Code Examples (10m)
    • will cover 3.0 and 2.7
    • (stub: I haven't decided what my example will be yet)
    • Is metaclassing wise? (2.5m)
    • There's nothing inherently wrong or bad about it. Furthermore, sometimes it's by far the best way to solve a problem.
    • Beware, though: Some people find metaclassing confusing.
    • "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." (Brian Kernighan)
    • Questions (5m)

    At 10:50am to 11:30am, Friday 9th March

    In E3, Santa Clara Convention Center

    Coverage video

  • The Art of Subclassing

    by Raymond Hettinger

    All problems have simple, easy-to-understand, logical wrong answers. Subclassing in Python is no exception. Avoid the common pitfalls and learn everything you need to know about making effective use of inheritance in Python.

    Avoid the common pitfalls and learn everything you need to know about how subclass in Python.

    • Overriding and extending
    • Calling your parents
    • The ellipse / circle problem
    • What does a subclass mean?
    • Liskov Substitution Principle
    • Open Closed Principle
    • Facts of life when subclassing builtin types
    • Cooperative Multiple Inheritance
    • Common subclassing patterns
    • Use of the double underscore

    At 11:30am to 12:10pm, Friday 9th March

    In E3, Santa Clara Convention Center

    Coverage video

  • Stop Writing Classes

    by Jack Diederich

    Classes are great but they are also overused. This talk will describe examples of class overuse taken from real world code and refactor the unnecessary classes, exceptions, and modules out of them.

    Classes must be nouns but not every noun must be a class. If your class only has two methods and one of them is init you probably meant to write a function.
    MuffinMail recently refactored their API; it went from 20 classes scattered in 22 modules down to 1 class just 15 lines long. It was a welcome change, but we'll further refactor that down to a single function 3 lines long.

    The Python stdlib is an example of a namespace that is relatively flat. You won't find packages that consist of a single module defining an exception, and you won't find many exceptions at all - just 165 kinds in 200k lines of code. That's a tiny ratio compared to most projects including Django.

    Of course there are things, like containers, that should be classes. As a final example we'll add a Heap type to the heapq module (admit it, you already have one in your utils.py).

    At 12:10pm to 12:40pm, Friday 9th March

    In E3, Santa Clara Convention Center

    Coverage video

  • Interfaces and Python

    by Eric Snow

    In 2.6, Python introduced the Abstract Base Classes. Before that we had "protocols" (and we still do). In this talk we'll look at how the general concept of interfaces fits into today's Python. We'll also look at some of the alternate proposals of the past, some of the controversies around ABCs, and the direction interfaces might go in the future.

    Talk Outline:

    • What are Interfaces? (3 min)
    • modeling strict abstraction
    • precedents in other languages
    • Interfaces in Python (6 min)
    • duck-typing
    • Python "protocols"
    • past proposals (PEP 245)
    • how Python "interfaces" are different
    • Newer Interface Support (11 min)
    • annotations
    • Abstract Base Classes
    • why run-time validation?
    • ABC vs. duck-typing
    • Third-party Libraries (5 min)
    • Peak's PyProtocols
    • zope.interface
    • Twisted
    • What Next? (3 min)
    • strict interfaces
    • compile-time validation
    • an example interface library

    For more comprehensive coverage of interfaces in Python, check out this reference: http://readthedocs.org/docs/refe...

    At 2:40pm to 3:20pm, Friday 9th March

    In E3, Santa Clara Convention Center

    Coverage video