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

What are new-style classes?

P: n/a
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!

Aug 28 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


There's a link on the left sidebar of http://docs.python.org

http://www.python.org/doc/newstyle.html

--
Robert Kern
rk***@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Aug 28 '05 #2

P: n/a
Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!

Older Pythons have a dichotomy between programmer-declared object
classes (from which subclasses can inherit) and the built-in object
classes (which just existed as built-in, but which could not be used as
the base of subclasses).

More recently the object hierarchy was redesigned, and now everything
inherits from object, so (if you know what you are doing) you can
implement subclasses of the built-in types such as dict, float and str.

Robert Kern has given you a link that explains the situation in detail.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Aug 28 '05 #3

P: n/a
On Sunday 28 August 2005 04:47 am, Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes. The simplest is "object", so the simplest newstyle class is:

class no_class(object):
pass

as opposed to the simplest "old style" object which didn't inherit at all:

class really_no_class:
pass

I would regard the latter as deprecated now, since it basically doesn't
buy you anything to use it. The only reason to hang on to old style
classes would seem to be to avoid breaking older code that relied on
details such as the order of multiple inheritence, which have changed.

So if you're just learning, just use new style classes exclusively, and
use the documentation that applies to them. I think it's fairly non-
controversial that new style classes are an improved design.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Aug 28 '05 #4

P: n/a
Terry Hancock wrote:
On Sunday 28 August 2005 04:47 am, Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes.


[Warning, advanced stuff ahead!]

That's not entirely true. New-style classes need not be derived from a new-
style class, they need to use the metaclass "type" or a derived.

So you can also declare a new-style class as

class new_class:
__metaclass__ = type

Or, if you want to switch a whole module with many classes to new-style, just set a

__metaclass__ = type

globally.
Reinhold
Aug 28 '05 #5

P: n/a
Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


In short: They have inherited "object" from somewhere. (This is
probably technically inaccurate, but it's the only thing I have seen
which defines a 'new-style class').

What it means *practically*, is that you can use properties. For a
long while I didn't really understand the point or properties, until I
needed them.

I have written a flowcharting application in Python. All objects on
the flowchat are derived from a base object which has the attribute
"text" (all objects have some form of, sometimes internal only, title).
Problem is that when some objects change text, I want to perform some
recalculations. So I wrote a "text" property, which - in its
settext-method - does all the calculations if required.

That way, the application can till use:

obj.text = 'Blah'

...and still have the chance to act on the text change.

I could have simply written a setText() method, but imho properties
are neater - but that's just a matter of opinion.
--
Kind Regards,
Jan Danielsson
Te audire no possum. Musa sapientum fixa est in aure.
Aug 29 '05 #6

P: n/a
Reinhold Birkenfeld wrote:
Terry Hancock wrote:
On Sunday 28 August 2005 04:47 am, Vaibhav wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!


"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes.

[Warning, advanced stuff ahead!]

That's not entirely true. New-style classes need not be derived from a new-
style class, they need to use the metaclass "type" or a derived.

So you can also declare a new-style class as

class new_class:
__metaclass__ = type

Or, if you want to switch a whole module with many classes to new-style, just set a

__metaclass__ = type

globally.
Reinhold

What are the pros and cons of the alternate approach?

Colin W.
Aug 30 '05 #7

P: n/a
Colin J. Williams wrote:
I recently heard about 'new-style classes'. I am very sorry if this
sounds like a newbie question, but what are they? I checked the Python
Manual but did not find anything conclusive. Could someone please
enlighten me? Thanks!

"New style" classes are becoming the standard in Python, and must
always be declared as a subclass of a new style class, including built-in
classes.

[Warning, advanced stuff ahead!]

That's not entirely true. New-style classes need not be derived from a new-
style class, they need to use the metaclass "type" or a derived.

So you can also declare a new-style class as

class new_class:
__metaclass__ = type

Or, if you want to switch a whole module with many classes to new-style, just set a

__metaclass__ = type

globally.

What are the pros and cons of the alternate approach?


The customary way is to use "class new_class(object):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.

Reinhold
Aug 30 '05 #8

P: n/a
On Tuesday 30 August 2005 04:09 pm, Reinhold Birkenfeld wrote:
The customary way is to use "class new_class(object):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.


Nonsense. "__metaclass__" is simply an implementation detail.

We know that because it begins with "__".

Therefore it is invisible, and any delusion you may have that
you can see it is a complete non-issue.

In Python we call that encapsulation.

;-D

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Aug 31 '05 #9

P: n/a
Terry Hancock wrote:
On Tuesday 30 August 2005 04:09 pm, Reinhold Birkenfeld wrote:
The customary way is to use "class new_class(object):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.
Nonsense.


Given the rest of your post, I assume that this isn't meant as it sounds. Remember, I'm
German, so please bear with my sense of humour. ;)
"__metaclass__" is simply an implementation detail.

We know that because it begins with "__".

Therefore it is invisible, and any delusion you may have that
you can see it is a complete non-issue.

In Python we call that encapsulation.

;-D


Reinhold
Aug 31 '05 #10

P: n/a
Reinhold Birkenfeld wrote:
Terry Hancock wrote:
On Tuesday 30 August 2005 04:09 pm, Reinhold Birkenfeld wrote:
The customary way is to use "class new_class(object):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.


Nonsense.

Given the rest of your post, I assume that this isn't meant as it sounds. Remember, I'm
German, so please bear with my sense of humour. ;)

German? Humour? Surely some mistake :-)

not-talking-about-the-war-ly y'rs - steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Aug 31 '05 #11

P: n/a
Steve Holden wrote:
Reinhold Birkenfeld wrote:
Terry Hancock wrote:
On Tuesday 30 August 2005 04:09 pm, Reinhold Birkenfeld wrote:

The customary way is to use "class new_class(object):". There's no advantage in using
__metaclass__ except that you can set it globally for all classes in that module
(which can be confusing on its own).

My comment mostly referred to "new-style classes must be declared as a subclass of
a new-style class", which is not true.

Nonsense.

Given the rest of your post, I assume that this isn't meant as it sounds. Remember, I'm
German, so please bear with my sense of humour. ;)

German? Humour? Surely some mistake :-)


Hm, should have added "if any" above...
not-talking-about-the-war-ly y'rs - steve


No surprise it was the Brits who discovered the world's deadliest joke.

Reinhold
Aug 31 '05 #12

P: n/a
On Wednesday 31 August 2005 12:14 pm, Reinhold Birkenfeld wrote:
Steve Holden wrote:
Reinhold Birkenfeld wrote:
>My comment mostly referred to "new-style classes must be declared as a subclass of
>a new-style class", which is not true.

Nonsense.

Given the rest of your post, I assume that this isn't meant as it sounds. Remember, I'm
German, so please bear with my sense of humour. ;)


Yes, it was tongue-in-cheek.

However, there *was* some truth in what I said:

Python *does* indicate implementation details with the
"ignore the man behind the curtain" approach of marking
them with "_" or "__".

Yes, that is the only kind of encapsulation Python really
gives you -- you have to play by the rules if you want
those advantages, and one of the rules is to pretend you
don't see those magic methods.

Except when you really, really need to, of course.

Also, we have to remember that this was a *newbie* question,
so I gave a newbie-approved answer -- I told the "right"
way to do it.

And of course, there is something slightly comical about
this. But you should expect that from a language named
after a comedy troupe, shouldn't you?

The fact that I did this because I didn't know about the
other way is totally unimportant! :-P

You are such a rude man for displaying your intellectual
superiority like that. Fie!

Actually, I was thinking seriously about trying to use the
exact wording of Douglas Adams from "Hitchhiker's" (approx):
"We have normality, therefore anything you still can't cope
with is your own problem". But it didn't quite fit, and
the other possible reference to the "Son of the Invisible
Man" skit*, was just way too obscure. I doubt you've ever
seen it.

*I think this was in "Amazon Women on the Moon", though it
may have been "Kentucky Fried Movie". Both are TV spoofs.

And, yes, of course, I'm glad you posted about this detail,
I learned something from it, which is why I read this list.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Aug 31 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.