class with invalid base class

A

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
....
Traceback (most recent call last):

# What's 'function'? Why is it called?
....
Traceback (most recent call last):

# 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)
........
Traceback (most recent call last):

# 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)
........
Traceback (most recent call last):

# Okay.... Don't know what's going on, so I'll
# just fiddle around a bit.
.... def __init__(self): pass
........
Traceback (most recent call last):
.... def __init__(self, a): pass
........
Traceback (most recent call last):

# Which is it; 4 given or 1 given? And
# int had 3 passed to it....
.... def __init__(self, **args): print "I have", args
........
I have {}
Traceback (most recent call last):

Comments?

Andrew
(e-mail address removed)
 
T

Thomas Heller

Andrew Dalke said:
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):


# 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:
.... return a + b
....Traceback (most recent call last):

Thomas
 
M

Michael Hudson

Thomas Heller said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top