Slight metaclass confusion

ben at transversal.com ben at transversal.com
Tue Sep 9 06:59:14 EDT 2003


I am slightly confused about the way metaclasses work, having read
"Metaclass Programming In Python, Parts 1 and 2"

I get the fact that the instance of a metaclass is a class, but in
this case I fail to see why the following does'nt work:

>>> class Meta(type):
...     def __str__(cls):
...             print "I am " + repr(cls)
... 
>>> 
>>> Class = Meta("Fish", (), {})
>>> Class.__str__()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: descriptor '__str__' of 'object' object needs an argument

Whereas this does:

>>> class Simple(object):
...     def __str__(self):
...             return "I am " + repr(self)
... 
>>> obj = Simple()
>>> obj.__str__()
I am <__main__.Simple object at 0x402f676c>

The tutorial does mention this (slightly), but doesn't make it clear
why this is happening. I am probably missing something obvious though!

Thanks for the help,

Ben
---




More information about the Python-list mailing list