Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

What are new-style classes?

1 view
Skip to first unread message

Vaibhav

unread,
Aug 28, 2005, 5:47:45 AM8/28/05
to
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!

Robert Kern

unread,
Aug 28, 2005, 6:25:12 AM8/28/05
to pytho...@python.org

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

Steve Holden

unread,
Aug 28, 2005, 9:50:30 AM8/28/05
to pytho...@python.org
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/

Terry Hancock

unread,
Aug 28, 2005, 3:11:34 PM8/28/05
to pytho...@python.org

"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

Reinhold Birkenfeld

unread,
Aug 28, 2005, 3:24:50 PM8/28/05
to
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

Jan Danielsson

unread,
Aug 29, 2005, 10:11:14 AM8/29/05
to

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.

Colin J. Williams

unread,
Aug 30, 2005, 11:54:27 AM8/30/05
to
What are the pros and cons of the alternate approach?

Colin W.

Reinhold Birkenfeld

unread,
Aug 30, 2005, 5:09:37 PM8/30/05
to
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

Terry Hancock

unread,
Aug 31, 2005, 10:13:30 AM8/31/05
to pytho...@python.org
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

Reinhold Birkenfeld

unread,
Aug 31, 2005, 12:36:36 PM8/31/05
to
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

Steve Holden

unread,
Aug 31, 2005, 1:02:36 PM8/31/05
to pytho...@python.org
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

Reinhold Birkenfeld

unread,
Aug 31, 2005, 1:14:06 PM8/31/05
to
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

Terry Hancock

unread,
Aug 31, 2005, 6:21:33 PM8/31/05
to pytho...@python.org
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.

0 new messages