Contribute Media
A thank you to everyone who makes this possible: Read More

Objects and classes in Python (and JavaScript)

Summary

[EuroPython 2011] Jonathan Fine - 23 June 2011 in "Training Pizza Napoli"

Description

Python has a well-thought out system for classes. Beginners can use it without understanding it all. Experts can use it to produce code that is both elegant and powerful (such as models and class-based views in Django). Python classes can be used for many purposes.

This tutorial arises from the author's experience in using classes to solve problems, such as:

  • Add custom methods and attributes to objects that are, at root, just an integer
  • Construct dictionaries whose values are functions
  • Construct classes on-the-fly
  • Implement JavaScript object semantics in Python (advanced topic)

The outline syllabus is:

  • Review of the class statement in Python (and decorators)
  • Subclassing built-in types such as int and tuple
  • How to define classes without using a class statement
  • How to define a dispatch dictionary using a class statement
  • Metaclasses as syntactic sugar for class construction
  • Metaclasses to provide new class semantics
  • Review of JavaScript object semantics
  • Using Python classes to implement JavaScript object semantics

For Guido on the history of Python classes see:

This tutorial is for Intermediate or Advanced Python programmers. (Beginners will find it very hard going.) The aim of the course is to explain exactly what happens when a class in constructed, and to learn ways of using this knowledge.

Improve this page