Discussion:
class with invalid base class
(too old to reply)
Andrew Dalke
2003-11-05 08:43:00 UTC
Permalink
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
... return a+b
...
... 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?
class Spam(1): pass
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: int() takes at most 2 arguments (3 given)
# what were the three given arguments?
# is it something I can redefine?
... def __getattr__(self, name):
... print "Trying to get", repr(name)
... raise AttributeError(name)
...
class Spam(Report()): pass
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: this constructor takes no arguments
# doesn't look like it. What if I derive from an instance
# derived from object?
... def __getattr__(self, name):
... print "Trying to get", repr(name)
... raise AttributeError(name)
...
class Spam(Report()): pass
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: default __new__ takes no parameters
# Okay.... Don't know what's going on, so I'll
# just fiddle around a bit.
... def __init__(self): pass
...
class Spam(ABCD()): pass
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: __init__() takes exactly 1 argument (4 given)
... def __init__(self, a): pass
...
class Spam(ABCD()): pass
...
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: __init__() takes exactly 2 arguments (1 given)
# Which is it; 4 given or 1 given? And
# int had 3 passed to it....
... def __init__(self, **args): print "I have", args
...
class Spam(XYZZY()): pass
...
I have {}
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: __init__() takes exactly 1 argument (4 given)
Comments?

Andrew
***@dalkescientific.com
Thomas Heller
2003-11-05 09:29:39 UTC
Permalink
Post by Andrew Dalke
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
... return a+b
...
... pass
...
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
... return a + b
...
Post by Andrew Dalke
print spam.__class__
<type 'function'>
Post by Andrew Dalke
spam.__class__("Spam", (spam,), {})
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: function() argument 1 must be code, not str
Thomas
Michael Hudson
2003-11-05 13:35:52 UTC
Permalink
Post by Thomas Heller
Post by Andrew Dalke
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
... return a+b
...
... pass
...
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...
Actually, I think it's type(baseclass) that gets called -- otherwise
you wouldn't be able to inherit from old-style classes! What's
changed since 2.2 is that type objects are now callable.

(Isn't it amazing how something as simple as metaclasses -- and they
*are* a simple idea -- can be *so* confusing? I wrote and rewrote
that paragraph at least three times...)

Cheers,
mwh
--
But since your post didn't lay out your assumptions, your goals,
or how you view language characteristics as fitting in with
either, you're not a *natural* candidate for embracing Design by
Contract <0.6 wink>. -- Tim Peters, giving Eiffel adoption advice
Loading...