class with invalid base class

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Dalke

    class with invalid base class

    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
    [color=blue][color=green][color=darkred]
    >>> def spam(a, b):[/color][/color][/color]
    .... return a+b
    ....[color=blue][color=green][color=darkred]
    >>> class Spam(spam):[/color][/color][/color]
    .... pass
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: function() argument 1 must be code, not str[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    # What's 'function'? Why is it called?
    [color=blue][color=green][color=darkred]
    >>> class Spam(1): pass[/color][/color][/color]
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: int() takes at most 2 arguments (3 given)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    # what were the three given arguments?
    # is it something I can redefine?
    [color=blue][color=green][color=darkred]
    >>> class Report:[/color][/color][/color]
    .... def __getattr__(sel f, name):
    .... print "Trying to get", repr(name)
    .... raise AttributeError( name)
    ....[color=blue][color=green][color=darkred]
    >>> class Spam(Report()): pass[/color][/color][/color]
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: this constructor takes no arguments[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    # doesn't look like it. What if I derive from an instance
    # derived from object?
    [color=blue][color=green][color=darkred]
    >>> class Report(object):[/color][/color][/color]
    .... def __getattr__(sel f, name):
    .... print "Trying to get", repr(name)
    .... raise AttributeError( name)
    ....[color=blue][color=green][color=darkred]
    >>> class Spam(Report()): pass[/color][/color][/color]
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: default __new__ takes no parameters[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    # Okay.... Don't know what's going on, so I'll
    # just fiddle around a bit.
    [color=blue][color=green][color=darkred]
    >>> class ABCD:[/color][/color][/color]
    .... def __init__(self): pass
    ....[color=blue][color=green][color=darkred]
    >>> class Spam(ABCD()): pass[/color][/color][/color]
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: __init__() takes exactly 1 argument (4 given)[color=blue][color=green][color=darkred]
    >>> class ABCD:[/color][/color][/color]
    .... def __init__(self, a): pass
    ....[color=blue][color=green][color=darkred]
    >>> class Spam(ABCD()): pass[/color][/color][/color]
    ....
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: __init__() takes exactly 2 arguments (1 given)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    # Which is it; 4 given or 1 given? And
    # int had 3 passed to it....
    [color=blue][color=green][color=darkred]
    >>> class XYZZY:[/color][/color][/color]
    .... def __init__(self, **args): print "I have", args
    ....[color=blue][color=green][color=darkred]
    >>> class Spam(XYZZY()): pass[/color][/color][/color]
    ....
    I have {}
    Traceback (most recent call last):
    File "<interacti ve input>", line 1, in ?
    TypeError: __init__() takes exactly 1 argument (4 given)[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Comments?

    Andrew
    dalke@dalkescie ntific.com


  • Thomas Heller

    #2
    Re: class with invalid base class

    "Andrew Dalke" <adalke@mindspr ing.com> writes:
    [color=blue]
    > 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
    >[color=green][color=darkred]
    >>>> def spam(a, b):[/color][/color]
    > ... return a+b
    > ...[color=green][color=darkred]
    >>>> class Spam(spam):[/color][/color]
    > ... pass
    > ...
    > Traceback (most recent call last):
    > File "<interacti ve input>", line 1, in ?
    > TypeError: function() argument 1 must be code, not str[color=green][color=darkred]
    >>>>[/color][/color]
    >
    > # What's 'function'? Why is it called?[/color]

    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:
    [color=blue][color=green][color=darkred]
    >>> def spam(a, b):[/color][/color][/color]
    .... return a + b
    ....[color=blue][color=green][color=darkred]
    >>> print spam.__class__[/color][/color][/color]
    <type 'function'>[color=blue][color=green][color=darkred]
    >>> spam.__class__( "Spam", (spam,), {})[/color][/color][/color]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: function() argument 1 must be code, not str[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    Thomas

    Comment

    • Michael Hudson

      #3
      Re: class with invalid base class

      Thomas Heller <theller@python .net> writes:
      [color=blue]
      > "Andrew Dalke" <adalke@mindspr ing.com> writes:
      >[color=green]
      > > 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
      > >[color=darkred]
      > >>>> def spam(a, b):[/color]
      > > ... return a+b
      > > ...[color=darkred]
      > >>>> class Spam(spam):[/color]
      > > ... pass
      > > ...
      > > Traceback (most recent call last):
      > > File "<interacti ve input>", line 1, in ?
      > > TypeError: function() argument 1 must be code, not str[color=darkred]
      > >>>>[/color]
      > >
      > > # What's 'function'? Why is it called?[/color]
      >
      > 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...[/color]

      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

      Comment

      Working...