New-style classes questions

K

Kalle Anke

I'm confused by the concepts of old-style vs new-style classes, I've read
most of the documents I found about this but it doesn't "click". Probably
because I wasn't around before 2.2.

Anyway, the reason for new style classes are to make the whole type/object
thing work better together. There are a number of new features etc.

I think my problem is when new-style classes are used, at first I thought
that all classes become new-style classes when I was using 2.4, but if I
understand things correctly I need to subclass an existing class (assuming
that this class does the same) for it to become a new-style class.

Have I understood this correctly?

If I have, then I've got the following question:
================================================
Then I could write:

class Foo( Object )

to make a new-style class, and

class FooA

to make an old-style class.


What is the reason for allowing both styles? (backwards compatibility??)

When I make my own classes should they always be new-style objects or are
there reasons for using old-style object?
================================================
 
D

Diez B. Roggisch

What is the reason for allowing both styles? (backwards compatibility??)

yes.
When I make my own classes should they always be new-style objects or are
there reasons for using old-style object?

No, use new style if you can - except from the rare cases where above
mentioned backwards compatibilty is needed. E.g. exceptions have to be
old-style, and for example omniorb needs old-style classes for corba
implementations.

Diez
 
G

Gerrit Holl

Diez said:

Note that there is another way to create new-style classes:

__metaclass__ = type

before the first class definition:
<type 'type'>

I like this. However, perhaps other people reading my source code won't
like it, because when they see 'class Foo:', they might expect an
old-style class. But it's so much better to type and to read, that I
prefer this.

Does the Python style guide have any severe penalty for using this?

regards,
Gerrit.
 
K

Kalle Anke

I like this. However, perhaps other people reading my source code won't
like it, because when they see 'class Foo:', they might expect an
old-style class. But it's so much better to type and to read, that I
prefer this.

I personally would find this more difficult to read but it's a matter of
taste I suppose.
Det finns inte dåligt väder, bara dåliga kläder.

Så sant som det var sagt


jem
 

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
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top