Questions to the FAQ

J

Josef Wolf

Hello!

I'm new to python, so my first readings were the tutorial and the FAQs.
I got confused by some FAQ entries. Maybe some expert can help to clear the
confusion?

- FAQ entry 6.5: "What is delegation?"

AFAICS, the example in this FAQ-entry should create a derived class from
the file class. Therefore, shouldn't the first line of this class
definition read "class UpperOut(file):"?

How come self.__outfile to be the base class?

- FAQ entry 6.6:

This FAQ entry makes a difference between new-style classes and classic
classes. An example is given for the new style and a hint is given how a
classic class would look like. But I can't see any difference in them:
The new-style example starts with "class Derived(Base):" and the hint for
the classic class is "class Derived(Base): ..." So what's the difference?

What am I missing here?
 
S

Stefan Seefeld

Josef said:
- FAQ entry 6.5: "What is delegation?"

AFAICS, the example in this FAQ-entry should create a derived class from
the file class. Therefore, shouldn't the first line of this class
definition read "class UpperOut(file):"?

no, because that wouldn't be delegation but inheritance. Delegation is
*not* about subclassing, but about delegating a task to another object.
How come self.__outfile to be the base class?

- FAQ entry 6.6:

This FAQ entry makes a difference between new-style classes and classic
classes. An example is given for the new style and a hint is given how a
classic class would look like. But I can't see any difference in them:
The new-style example starts with "class Derived(Base):" and the hint for
the classic class is "class Derived(Base): ..." So what's the difference?

the example is about how an overridden method can call the base-class
method. For both cases the same child/parent relationship is assumed.
The difference is in the spelling of "call the base' class method",
which, with new-style classes uses 'super'.

Regards,
Stefan
 
P

Peter Hansen

Josef said:
Hello!

I'm new to python, so my first readings were the tutorial and the FAQs.
I got confused by some FAQ entries. Maybe some expert can help to clear the
confusion?

I'll leave it to the real experts to clear confusion. I'm
only expert in creating it. :-(

What I wanted to say, however, is "Thank you!" Thank you for
actually taking the time to do the tutorial and read the FAQs.
Few enough people, even those who aren't new to Python, seem
to bother taking the time. It's very nice to hear from someone
who has.

-Peter
 
J

John Lenton

I'm new to python, so my first readings were the tutorial and the FAQs.
I got confused by some FAQ entries. Maybe some expert can help to clear the
confusion?

- FAQ entry 6.5: "What is delegation?"

AFAICS, the example in this FAQ-entry should create a derived class from
the file class. Therefore, shouldn't the first line of this class
definition read "class UpperOut(file):"?

How come self.__outfile to be the base class?

no, UpperOut isn't a file, it _has_ a file. It passes all messages on
to the file, except for write, which it mangles a bit before passing
it on, too. You could do that with subclassing too, now that you can
subclass file:

class UpperOut(file):
def write(self, s):
super(UpperOut, self).write(s.upper())

which takes you nicely back to your next question.
 
S

Scott David Daniels

Josef said:
- FAQ entry 6.6:

This FAQ entry makes a difference between new-style classes and classic
classes. An example is given for the new style and a hint is given how a
classic class would look like. But I can't see any difference in them:
The new-style example starts with "class Derived(Base):" and the hint for
the classic class is "class Derived(Base): ..." So what's the difference?

What am I missing here?

OK, I don't know where this comes in the FAQs (if it is there at all).
First, the shorter answer:

You can't have a new-style class inherit from a classic class, nor can
you have a new-style inherit from a classic class. So, the "style" of
a class (classic or new-style) that inherits from some other class is
inherited.

Here is a useful lie (lie in that there are tricky ways to avoid this):
All new-style classes eventually inherit from the builtin class
"object", none of the old-style classes are subclasses of object.

So, for your question, after "class Derived(Base): ..."
if issubclass(Base, object): Derived is new-style.
if not issubclass(Base, object): Derived is classic.

or (perhaps interesting, perhaps more to the point, perhaps confusing):
if isinstance(Base, type): Derived is new-style.
if not isinstance(Base, type): Derived is classic.

Now a longer blather:

The difference between "classic classes" (or old-style classes as we
will eventually call them) is which "metaclass" they are built with.
Don't worry too much about that, you shouldn't fiddle with (or worry
about understanding) metaclasses until you know quite a lot about
Python's implementation. The nickel explanation is that a metaclass
controls how a class works in the same way that a class controls how
an instance works. The class controls how the instance behaves, and
the metaclass controls how that class does that controlling. "type"
is a class for types, which is how the second pair of tests above work.
object is the simplest class of type "type" -- everything below it
is a new-style class.

There are ways other than inheriting eventually from object to set
up a metaclass to use for a class, but you should regard that as black
magic for a while -- a metaclass gives you enough rope to shoot
yourself in the foot.
 
J

John Lenton

You can't have a new-style class inherit from a classic class, nor can
you have a new-style inherit from a classic class. So, the "style" of
a class (classic or new-style) that inherits from some other class is
inherited.

I'm just nitpicking, I know, but I think this isn't exact: you can
have a new-style inherit from a classic class:

Python 2.3.4 (#2, Jul 5 2004, 09:15:05)
[GCC 3.3.4 (Debian 1:3.3.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.<type 'classobj'>
 
S

Scott David Daniels

John said:
I'm just nitpicking, I know, but I think this isn't exact: you can
have a new-style inherit from a classic class:
OK, I was trying to be simple enough to be understood, and I got too
simple.
 
J

Josef Wolf

First of all, thanks to all who responded. I think I begin to understand
what's going on here.

Stefan Seefeld wrote in said:
no, because that wouldn't be delegation but inheritance. Delegation is
*not* about subclassing, but about delegating a task to another object.

Ah, _now_ i see the difference...

John Lenton wrote in said:
no, UpperOut isn't a file, it _has_ a file. It passes all messages on
to the file, except for write, which it mangles a bit before passing
it on, too.

So to instantiate it one would use "foo=UpperOut(some_file_object)"?
 
J

Josef Wolf

Scott David Daniels wrote in said:
You can't have a new-style class inherit from a classic class, nor can
you have a new-style inherit from a classic class. So, the "style" of
========= =============
\ /
swapped here?
a class (classic or new-style) that inherits from some other class is
inherited.

Here is a useful lie (lie in that there are tricky ways to avoid this):
All new-style classes eventually inherit from the builtin class
"object", none of the old-style classes are subclasses of object.

Mybe this one and ...

Stefan Seefeld wrote in said:
the example is about how an overridden method can call the base-class
method. For both cases the same child/parent relationship is assumed.
The difference is in the spelling of "call the base' class method",
which, with new-style classes uses 'super'.

.... this one could be added to the FAQ?

Thanks!
 

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,662
Latest member
salsusa

Latest Threads

Top