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
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/
"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
[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
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 W.
>>>>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
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
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
not-talking-about-the-war-ly y'rs - steve
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
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.