TITLE OF PAPER: New Style Class Tutorial URL OF PRESENTATION: _URL_of_powerpoint_presentation_ PRESENTED BY: Thomas Wouters REPRESENTING: _name_of_the_company_they_represent_ CONFERENCE: PyCON 2004 DATE: 20040325 LOCATION: _venue_and_room_in_venue_ -------------------------------------------------------------------------- REAL-TIME NOTES / ANNOTATIONS OF THE PAPER: {If you've contributed, add your name, e-mail & URL at the bottom} [...missed first 10 minutes] Subclassing C types in Python just works (mostly) - Cannot subclass from multiple C types if they have incompatible memory layouts - And it's not always a good idea... Creating immutable types: uses __new__, Python's constructor - it is a *static* method, called with the class as the first argument - responsible for obtaining memory - may return an existing value - may defined __slots__ instead of (or along with) __dict__ - avoids memory overhead of dict if not used Subclassing C types in C - complex, but now possible - make sure base type supports subclassing - no type-specific hardcoding, among other reqts Type checking: a necessary evil in C - Define Py_Check() in terms of PyObject_TypeCheck() - Can also have Py_CheckExact() - ptr comparison against actual type struct _ Definitions for members: - PyMemberDef (tp_members) for instance data - PyMethodDef (tp_methods) for methods - wraps the C functions in Python [descriptor] objects - PyGetSetDef (tp_getset) for accessors [properties] - [these tp_ fields end up pointing to arrays of these def objects] - - notes: - tp_flags must have Py_TPFLAGS_BASETYPE set to be subclassed - tp_type, tp_base filled in during module initialization (tp_type via PyType_Ready()) Subclassing a struct: - Include base class struct as first field in subclass struct - ramifications for multiple inheritance Method Resolution Order (MRO): - Old MRO also had no convenient way to access base classes - New MRO algorithm uses "C3" algorithm from Dylan - Essentially same order as before, but eliminates all but last occurance of class - example: in diamond inheritance: A -> B, C -> D - was: D B A C A - now: D B C A - Correctly eliminates multiple calls to root base class super(): - Proxy object for base class of current object - Continues MRO where it left off - finds the method that "would have been called" if your class wasn't present - "Do it for your children" Descriptors: - objects with __get__, __set__, __delete__ methods that get called by getattr/setattr/delattr - example: caching property - Python properties are an application of descriptors - also hold docstrings for attributes - set and delete don't work with old-style classes - demonstration of example Special method types: - classmethod - takes class as implicit first argument (not instance) - can be called through the class or an instance (object) - allows e.g. for factory functions that may return subclass objects - staticmethod - takes no implicit first argument - turns out to be less necessary than originally thought - was designed for calling __new__ - can use super() for this - but allows for having a (non-method) function as a member of a class - [like static members in Java, C++, etc.] - example: - Note use of builtin funcs classmethod()/staticmethod() - [This is partly why the decorator syntax PEP is desired] Metaclasses: - the class of a class (usually derives from type()) - Can "post-process" a class - Assign meta-class to __metaclass__ attribute in class definition - [this can make your brain hurt pretty quickly...] Example: "my favorite metaclass" 'conveniencyTypeType' - automethod(func) turns the func into the right method type (class, static) by looking at the first argument name ("cls", "self") and doing the "right thing" - metaclass itself automagically creates self.__super that acts like super(self) - also turns appropriately named methods into a property[?] -------------------------------------------------------------------------- REFERENCES: {as documents / sites are referenced add them below} [reference to tp_flags in python doc?] [reference to MRO in python doc?] Slides: http://www.xs4all.nl/~thomas/python/ [example code will be there too] -------------------------------------------------------------------------- QUOTES: "Do it for your children" -------------------------------------------------------------------------- CONTRIBUTORS: {add your name, e-mail address and URL below} Russell Finn, rsf@sprucehill.com, http://www.sprucehill.com/rsf/blog/ [future] -------------------------------------------------------------------------- E-MAIL BOUNCEBACK: {add your e-mail address separated by commas } -------------------------------------------------------------------------- NOTES ON / KEY TO THIS TEMPLATE: A headline (like a field in a database) will be CAPITALISED This differentiates from the text that follows A variable that you can change will be surrounded by _underscores_ Spaces in variables are also replaced with under_scores This allows people to select the whole variable with a simple double-click A tool-tip is lower case and surrounded by {curly brackets / parentheses} These supply helpful contextual information. -------------------------------------------------------------------------- Copyright shared between all the participants unless otherwise stated...