What are new-style classes?

V

Vaibhav

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!
 
R

Robert Kern

Vaibhav said:
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
(e-mail address removed)

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

Steve Holden

Vaibhav said:
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
 
T

Terry Hancock

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

Reinhold Birkenfeld

Terry said:
"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
 
J

Jan Danielsson

Vaibhav said:
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.
 
C

Colin J. Williams

Reinhold said:
Terry said:
"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.
 
R

Reinhold Birkenfeld

Colin said:
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
 
T

Terry Hancock

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
 
R

Reinhold Birkenfeld

Terry said:
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
 
S

Steve Holden

Reinhold said:
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
 
T

Terry Hancock

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.
 

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