Women in Technology

Hear us Roar



Article:
  A Primer on Python Metaclass Programming
Subject:   Example 4 elaboration?
Date:   2003-12-11 16:28:14
From:   shalabh
When explaining example 4, the text mentions "The first argument to methods is conventionally called cls rather than self, because the methods operate on the produced class, not the metaclass".


Perhaps this doesn't tell the whole story. The cls in __new__ and cls in __init__ are actually different. While the produced class (__main__.X in the example) is passed as the first parameter to __init__, the metaclass (ChattyClass in the example) is passed as first parameter to the __new__ method. This can be verified with a simple print statement in __new__().