class with invalid base class

Thomas Heller theller at python.net
Wed Nov 5 04:29:39 EST 2003


"Andrew Dalke" <adalke at mindspring.com> writes:

> Out of curiosity, I tried passing using an invalid base
> for a class.  I can't explain why I got the error messages
> I did.  Can someone here enlighten me?
>
>    # Here I'm just curious
>
>>>> def spam(a, b):
> ...  return a+b
> ...
>>>> class Spam(spam):
> ...  pass
> ...
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: function() argument 1 must be code, not str
>>>>
>
>   # What's 'function'?  Why is it called?

It's the Don Beaudry hook ;-).  If the base class has callable a
__class__ attribute, this is called with three arguments: The name of
the new class, a tuple of the bases, and a dictionary.  Or something like
this, the details may be wrong...

Since now functions have a __class__ attribute, the hook is triggered by
your code above:

>>> def spam(a, b):
...     return a + b
...
>>> print spam.__class__
<type 'function'>
>>> spam.__class__("Spam", (spam,), {})
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: function() argument 1 must be code, not str
>>>

Thomas




More information about the Python-list mailing list