class with invalid base class

Andrew Dalke adalke at mindspring.com
Wed Nov 5 03:43:00 EST 2003


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?

>>> 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?

>>> class Report:
...  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?

>>> class Report(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.

>>> class ABCD:
...  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)
>>> class ABCD:
...  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....

>>> class XYZZY:
...  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
                    dalke at dalkescientific.com






More information about the Python-list mailing list