class with invalid base class

Discussion in 'Python' started by Andrew Dalke, Nov 5, 2003.

  1. Andrew Dalke

    Andrew Dalke Guest

    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
     
    Andrew Dalke, Nov 5, 2003
    #1
    1. Advertising

  2. "Andrew Dalke" <> 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
     
    Thomas Heller, Nov 5, 2003
    #2
    1. Advertising

  3. Thomas Heller <> writes:

    > "Andrew Dalke" <> 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...


    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
     
    Michael Hudson, Nov 5, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tomek
    Replies:
    2
    Views:
    705
    Andrey Tarasevich
    Dec 1, 2003
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    571
    John Carson
    Sep 3, 2005
  3. kevin
    Replies:
    0
    Views:
    1,031
    kevin
    Jan 16, 2008
  4. Hicham Mouline
    Replies:
    1
    Views:
    625
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    155
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page