any ways to judge whether an object is initilized or not in a class

A

Alex Martelli

Steven D'Aprano said:
There are plenty of reasons for preferring new style classes. If those
reasons hold for you, then of course you should use new style classes.

But that's not the same thing as saying that you should use new style
classes *even when you don't care about those features*.

You should always use new-style classes in order to avoid having to stop
and make a decision each time you code a class -- having to stop and ask
yourself "do I need any of the many extra features of new-style classes
here, or will legacy classes suffice?" each and every time.

There should ideally be only one obvious way -- and that obvious way is
to always use new-style classes and avoid a feature that's there only
for backwards compatibility with legacy code.

It's a specific case of the general rule "adopt good habits as
routines"; I recommend Limoncelli's excellent treatment of that subject
in his "Time Management for System Administrators" book. By not having
to ask yourself "do I really need to do X" each and every time, but
making it an absolute and invariant habit to always do it, as long as X
is a good habit (will be useful some of the time and not damaging the
rest of the time), you save yourself time-waste, aggravation, and
useless expenditure of attention and mental energy.
What I predict is that under the hood, Python 3 will complete the job of
unifying types and classes. The distinction between classic classes and
new style classes will go away. All classes will behave the same, whether
you write "class X:" or "class X():" or "class X(object):" or whatever
syntax Python 3 uses for defining classes.

Sure -- all the semantical peculiarity of legacy classes will disappear
(and won't be missed). But meanwhile, no reason not to adopt good
habits.


Alex
 
S

Steven D'Aprano

You should always use new-style classes in order to avoid having to stop
and make a decision each time you code a class -- having to stop and ask
yourself "do I need any of the many extra features of new-style classes
here, or will legacy classes suffice?" each and every time.

I can sympathize with your argument. I have taught myself to _always_
indicate when turning the car, even when I don't need to. I do it without
thinking, even when turning from my own driveway into my garage (much to
my wife's amusement). And that way, I don't have to think on the road "do
I need to indicate now or not?", I just do it.

Rather than keep arguing my case (life is short...) I'll just mention that
both Fredrik Lundh and Aahz were very dismissive of the suggestion that
people should stop using classic classes back in July 2005.

http://www.thescripts.com/forum/thread25853.html

e.g. Aahz wrote: "There's a big difference between being gung-ho on
new-style classes and telling people to stop using old-style classes."

Have they changed their mind since then? Not that I'm suggesting that
their opinions outweigh Alex's, but I'm not sure that the situation
vis-a-vis classes has changed that much since 2005. If it has, perhaps I
should be revising my opinion too.
 
A

Aahz

I can sympathize with your argument. I have taught myself to _always_
indicate when turning the car, even when I don't need to. I do it without
thinking, even when turning from my own driveway into my garage (much to
my wife's amusement). And that way, I don't have to think on the road "do
I need to indicate now or not?", I just do it.

Rather than keep arguing my case (life is short...) I'll just mention that
both Fredrik Lundh and Aahz were very dismissive of the suggestion that
people should stop using classic classes back in July 2005.

http://www.thescripts.com/forum/thread25853.html

e.g. Aahz wrote: "There's a big difference between being gung-ho on
new-style classes and telling people to stop using old-style classes."

Have they changed their mind since then? Not that I'm suggesting that
their opinions outweigh Alex's, but I'm not sure that the situation
vis-a-vis classes has changed that much since 2005. If it has, perhaps I
should be revising my opinion too.

My mind has not changed, and it has since been enshrined in _Python for
Dummies_. ;-) One point that I should make clear, because I think it
plays a large part of why Fredrik and I differ from Alex: my code at work
still runs on Python 2.2, which I believe should be avoided for heavy use
of new-style classes. (The differences in new-style classes between 2.2
and later versions are small but significant, and I *don't* want to have
to think about them because we also use 2.3 at work.) I believe Fredrik
still supports Python 1.5.2. If you only support 2.3 and later,
suggesting a complete switchover to new-style classes becomes more
reasonable.

Moreover, my codebase at work started with Python 1.4, and upgrading it
all to correctly work with new-style classes would be a monumental
undertaking. From my POV, as long as the bulk of the standard library is
still based on classic classes, that undertaking is not worth it.

I'm not quite as habitual as you are about turn signals, but I'm pretty
similar...
 
A

Aahz

Not officially yet, true. But come on, it's been 6 (six) years since
type unification, and it has always been obvious (to me at least) that
the new object model was to replace the 'classic' one.

....in Python 3.0. Anything else you believe is strictly a product of
your imagination.
 
B

Bruno Desthuilliers

Steven D'Aprano a écrit :
On Tue, 20 Mar 2007 10:28:10 +0100, Bruno Desthuilliers wrote:

[deploying weapons of mass snippage]

Lol !-)
Yes, it is my opinion, and it seems that in your zeal to defend new-style
classes against an imaginary attack, you've completely misunderstood what
my opinion is.

Your opinion, as I understood it, is that it's not worth six extra
keystrokes (in the worse case) to get the full power of Python's object
model.
I'm not against new-style classes. I do use new-style classes.

So why do you use old-style ones too ? Seems like it would be simpler to
stick to new-style whatever, no ?
There are a
whole lot of extra features that new-style classes have that old-style
classes don't have, some of which I didn't even think of. (Thanks for
pointing them out, and I'm not being sarcastic.)

There are plenty of reasons for preferring new style classes. If those
reasons hold for you, then of course you should use new style classes.

But that's not the same thing as saying that you should use new style
classes *even when you don't care about those features*.

Could we see it the other way round ? Everything you can do with
old-style classes (except writing code compatible with more than
six-years old python interpreters), you can do with new-styles. So why
even wasting time asking yourself if you need these features now or will
need them later ? Saving six extra keystrokes ? How much time do you
need to type 'object' ? Certainly less than 1 second. And how much time
will you spend when you'll find out that you finally need some of these
'extra' features ?
I never once suggested that new style classes are unnecessary, or a waste
of time, or bad, or whatever else you seem to think I was saying. My point
was, if you don't _need_ a new style class, there is no reason to avoid
using an old style class. It is a purely personal choice.

My point is that there's no reason to keep on using old-style classes
(compatibility with six-years old interpreters set aside).
There seems to be a misunderstanding that classic classes have been
depreciated. They certainly have not.

Not officially yet, true. But come on, it's been 6 (six) years since
type unification, and it has always been obvious (to me at least) that
the new object model was to replace the 'classic' one.
 
A

Antoon Pardon

You should always use new-style classes in order to avoid having to stop
and make a decision each time you code a class -- having to stop and ask
yourself "do I need any of the many extra features of new-style classes
here, or will legacy classes suffice?" each and every time.

There should ideally be only one obvious way -- and that obvious way is
to always use new-style classes and avoid a feature that's there only
for backwards compatibility with legacy code.

I disagree. The obvious way is to use old-style classes. IMO the obvious
way, is what a newbee will do after he has read the tutorial. And as far
as I am familiar with the tutorial someone trying out python after he
read it, will use old-style classes.
 

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,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top