views:

387

answers:

2

I tried to subclass threading.Condition earlier today but it didn't work out. Here is the output of the Python interpreter when I try to subclass the threading.Condition class:

>>> import threading
>>> class ThisWontWork(threading.Condition):
...     pass
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
    function() argument 1 must be code, not str

Can someone explain this error? Thanks!

+2  A: 

You're getting that exception because, despite its class-like name, threading.Condition is a function, and you cannot subclass functions.

>>> type(threading.Condition)
<type 'function'>

This not-very-helpful error message has been raised on the Python bugtracker, but it has been marked "won't fix."

Will McCutchen
Strange, I didn't think to check its type. The docs seemt o be a bit misleading then because they say (http://docs.python.org/library/threading.html) "class threading.Condition([lock])" which seems a bit misleading.Anyway, thanks for clearing this up :).
David Underhill
+1  A: 

Different problem than OP had, but you can also get this error if you try to subclass from a module instead of a class (e.g. you try to inherit My.Module instead of My.Module.Class). Kudos to this post for helping me figure this out.

Von