By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

New-style classes questions

P: n/a
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?
================================================
Sep 25 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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
Sep 25 '05 #2

P: n/a
Diez B. Roggisch wrote:
What is the reason for allowing both styles? (backwards compatibility??)


yes.


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

__metaclass__ = type

before the first class definition:
class Foo: pass .... type(Foo) <type 'classobj'> __metaclass__ = type
class Bar: pass .... type(Bar)

<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.

--
Temperature in Luleå, Norrbotten, Sweden:
| Current temperature 05-09-25 15:19:47 11.0 degrees Celsius ( 51.9F) |
--
Det finns inte dåligt väder, bara dåliga kläder.
Sep 25 '05 #3

P: n/a
On Sun, 25 Sep 2005 15:24:29 +0200, Gerrit Holl wrote
(in article <ma************************************@python.org >):
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

Sep 25 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.