473,326 Members | 2,126 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Why new Python 2.5 feature "class C()" return old-style class ?

For Python developers around.
From Python 2.5 doc:

The list of base classes in a class definition can now be empty. As an
example, this is now legal:
class C():
pass

nice but why this syntax return old-style class, same as "class C:",
and not the new style "class C(object):" ?
Old-style class are somewhat deprecated and could be almost always be
replaced by new-style class, so this syntax could be a nice shortcut to
create them.

Am I wrong or is there something that I've missed ?

Apr 11 '06 #1
38 1993
looping wrote:
For Python developers around.
From Python 2.5 doc:

The list of base classes in a class definition can now be empty. As an
example, this is now legal:
class C():
pass

nice but why this syntax return old-style class, same as "class C:",
and not the new style "class C(object):" ?
Old-style class are somewhat deprecated and could be almost always be
replaced by new-style class, so this syntax could be a nice shortcut to
create them.

Am I wrong or is there something that I've missed ?


class C():

is meant to be synonymous with

class C:

and therefore cannot create a new-style class.

Georg
Apr 11 '06 #2
Georg Brandl wrote:
class C():

is meant to be synonymous with

class C:

and therefore cannot create a new-style class.


I think "looping" understands that, but is basically asking why anyone
is bothering with a change that involves a part of the language that is
effectively deprecated. In other words, class(): never used to be
valid, so why make it valid now?

-Peter

Apr 11 '06 #3
Peter Hansen wrote:
Georg Brandl wrote:
class C():

is meant to be synonymous with

class C:

and therefore cannot create a new-style class.


I think "looping" understands that, but is basically asking why anyone
is bothering with a change that involves a part of the language that is
effectively deprecated. In other words, class(): never used to be
valid, so why make it valid now?


I don't recall that, you'll have to search the python-dev archives.

Georg
Apr 11 '06 #4
Peter Hansen wrote:
Georg Brandl wrote:
class C():

is meant to be synonymous with

class C:

and therefore cannot create a new-style class.


I think "looping" understands that, but is basically asking why anyone
is bothering with a change that involves a part of the language that is
effectively deprecated. In other words, class(): never used to be
valid, so why make it valid now?

-Peter


Exact.
But I think that if we make "class C():" a synonym of "class
C(object):", it will save lot of keystrokes ;-)
So I think the idea is great but the result is not actually very
usefull.

Delphi (Pascal?) use almost the same concept:
TTest = class

is a synonym of

TTest = class(TObject)

Apr 11 '06 #5
looping wrote:
Peter Hansen wrote:
Georg Brandl wrote:
class C():

is meant to be synonymous with

class C:

and therefore cannot create a new-style class.


I think "looping" understands that, but is basically asking why anyone
is bothering with a change that involves a part of the language that is
effectively deprecated. In other words, class(): never used to be
valid, so why make it valid now?

-Peter

Exact.
But I think that if we make "class C():" a synonym of "class
C(object):", it will save lot of keystrokes ;-)


Since the class statement without superclass actually creates an
old-style class, I'd expect the "class MyClass():" variant to behave
the same. Sacrifying readability and expliciteness just to save half a
dozen keystrokes is not pythonic IMHO.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 11 '06 #6
bruno at modulix>Since the class statement without superclass actually
creates an old-style class, I'd expect the "class MyClass():" variant
to behave the same.<

In Python 3.0 I really hope the

class C: pass
class C(): pass
class C(object): pass

will mean the same thing. (So in Python 2.5 the second version can be
made to mean the same thing of the third).

Bye,
bearophile

Apr 11 '06 #7
bruno at modulix wrote:
looping wrote:
Peter Hansen wrote:
Georg Brandl wrote:

class C():

is meant to be synonymous with

class C:

and therefore cannot create a new-style class.

I think "looping" understands that, but is basically asking why anyone
is bothering with a change that involves a part of the language that is
effectively deprecated. In other words, class(): never used to be
valid, so why make it valid now?

-Peter

Exact.
But I think that if we make "class C():" a synonym of "class
C(object):", it will save lot of keystrokes ;-)


Since the class statement without superclass actually creates an
old-style class, I'd expect the "class MyClass():" variant to behave
the same. Sacrifying readability and expliciteness just to save half a
dozen keystrokes is not pythonic IMHO.


I don't think readability suffer and expliciteness could sometimes be
sacrified to simplify the life of developer: ex "abcd"[0:3] ->
"abcd"[:3].
And for newbies, the somewhat magic behavior of the "object" superclass
is not so clear even that it is very explicit.
When I write script I don't use new-style class cause is bother me to
type "(object)" when I don't need their features. But in an other hand,
I believe that new-style class are faster to instanciate (maybe I'm
wrong...).
So this new syntax is a good way to boost their uses without bother
with compatibility of existing code IMHO.

Well I stop to argue now and let Python Dev make their (very good) job.

Apr 11 '06 #8
looping wrote:
Peter Hansen wrote:
Georg Brandl wrote:
> class C():
>
> is meant to be synonymous with
>
> class C:
>
> and therefore cannot create a new-style class.


I think "looping" understands that, but is basically asking why anyone
is bothering with a change that involves a part of the language that is
effectively deprecated. In other words, class(): never used to be
valid, so why make it valid now?

-Peter


Exact.
But I think that if we make "class C():" a synonym of "class
C(object):", it will save lot of keystrokes ;-)


If you have many classes in a module, putting

__metaclass__ = type

at the top can save you these keystrokes.

Georg
Apr 11 '06 #9
Em Ter, 2006-04-11 Ã*s 06:49 -0700, looping escreveu:
But in an other hand,
I believe that new-style class are faster to instanciate (maybe I'm
wrong...).


$ python2.4 -m timeit -s 'class x: pass' 'x()'
1000000 loops, best of 3: 0.435 usec per loop
$ python2.4 -m timeit -s 'class x(object): pass' 'x()'
1000000 loops, best of 3: 0.316 usec per loop

--
Felipe.

Apr 11 '06 #10
In article <11**********************@g10g2000cwb.googlegroups .com>,
<be************@lycos.com> wrote:

In Python 3.0 I really hope the

class C: pass
class C(): pass
class C(object): pass

will mean the same thing.
The BDFL made that one of the very first Pronouncements of 3.0. ;-)
(So in Python 2.5 the second version can be made to mean the same thing
of the third).


Can, yes. But should it? The whole point of adding the () option to
classes was to ease the learning process for newbies who don't
understand why classes have a different syntax from functions. Having

class C(): pass

behave differently from

class C: pass

would be of no benefit for that purpose.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"
Apr 11 '06 #11
be************@lycos.com wrote:
bruno at modulix>Since the class statement without superclass actually
creates an old-style class, I'd expect the "class MyClass():" variant
to behave the same.<

In Python 3.0 I really hope the

class C: pass
class C(): pass
class C(object): pass

will mean the same thing.


Yes, but this is for 3.0. Actually we're still at 2.5.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 11 '06 #12
looping wrote:
bruno at modulix wrote:
looping wrote:
Peter Hansen wrote:
Georg Brandl wrote:
>class C():
>
>is meant to be synonymous with
>
>class C:
>
>and therefore cannot create a new-style class.

I think "looping" understands that, but is basically asking why anyone
is bothering with a change that involves a part of the language that is
effectively deprecated. In other words, class(): never used to be
valid, so why make it valid now?

-Peter
Exact.
But I think that if we make "class C():" a synonym of "class
C(object):", it will save lot of keystrokes ;-)
Since the class statement without superclass actually creates an
old-style class, I'd expect the "class MyClass():" variant to behave
the same. Sacrifying readability and expliciteness just to save half a
dozen keystrokes is not pythonic IMHO.

I don't think readability suffer


It does. The statement "class X():" imply there's no superclass, so it
definitiveley should behave the same as "class X:".
and expliciteness could sometimes be
sacrified to simplify the life of developer: ex "abcd"[0:3] ->
"abcd"[:3].
Here there's no ambiguity.
And for newbies, the somewhat magic behavior of the "object" superclass
is not so clear even that it is very explicit.
There's no magic involved here. And I really doubt that having
inconsistant behaviour for "class X():" wrt/ "class X:" will help here.
When I write script I don't use new-style class
You should.
cause is bother me to
type "(object)" when I don't need their features.
Please repeat this 101 times each morning:
"thou shall not use old-style classes for they are deprecated".

(snip)
So this new syntax is a good way to boost their uses without bother
with compatibility of existing code IMHO.


It's mostly a good way to add inconsistency and confusion to a situation
that's already confusing enough for newbies.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Apr 11 '06 #13

Georg Brandl wrote:
If you have many classes in a module, putting

__metaclass__ = type

at the top can save you these keystrokes.

Georg


We could do that ? Nice trick that I've never thoughts about myself.

Thanks Georg.

Apr 11 '06 #14
bruno at modulix wrote:
cause is bother me to
type "(object)" when I don't need their features.


Please repeat this 101 times each morning:
"thou shall not use old-style classes for they are deprecated".


It's a pity though that Python still uses old-style classes internally,
even for some things which are brand new:
from __future__ import with_statement
type(with_statement)

<type 'instance'>

(yes, I know that the _Feature class isn't actually new, just this
particular instance of it.)
Apr 11 '06 #15
In article <44***********************@news.free.fr>,
bruno at modulix <on***@xiludom.gro> wrote:

Please repeat this 101 times each morning:
"thou shall not use old-style classes for they are deprecated".


Classic classes are *NOT* deprecated. And Python for Dummies will make
that clear (though we will note that there are people in the community
who believe as you do).
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"
Apr 11 '06 #16
Aahz wrote:
The*whole*point*of*adding*the*()*option*to
classes was to ease the learning process for newbies who don't
understand why classes have a different syntax from functions.


That cuts both ways. Now a determined newbie won't understand why

def f: pass

and

bases = A, B
class C(*bases): pass

don't work as expected...

Peter

Apr 11 '06 #17
Aahz wrote:
Classic classes are *NOT* deprecated.

I'm surprised ...
So there will be two (in most cases subtly) different classes of classes
(so to speak) for all eternity?

Why is that? Do classic classes have some advantage over new style ones?
If so, what are they?

eagerly:
wildemar
Apr 11 '06 #18
Em Ter, 2006-04-11 Ã*s 07:17 -0700, Aahz escreveu:
Can, yes. But should it? The whole point of adding the () option to
classes was to ease the learning process for newbies who don't
understand why classes have a different syntax from functions. Having

class C(): pass

behave differently from

class C: pass

would be of no benefit for that purpose.


Why should a newbie use an old-style class?

--
Felipe.

Apr 11 '06 #19
Wildemar Wildenburger wrote:
Aahz wrote:
Classic classes are *NOT* deprecated.
I'm surprised ...
So there will be two (in most cases subtly) different classes of classes
(so to speak) for all eternity?


No, Python 3.0, the backwards-compatibility-breaking release, will remove them.
"Deprecated" has a rather specific meaning for Python language features and
doesn't really apply to classic classes, yet.

http://www.python.org/dev/peps/pep-0005/
Why is that? Do classic classes have some advantage over new style ones?
If so, what are they?


There's a slight speed advantage in some places, but nothing you should worry
about. I, at least, would recommend always using new classes in new code unless
if you are sure you need classic classes.

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 11 '06 #20
In article <ma***************************************@python. org>,
Felipe Almeida Lessa <fe**********@gmail.com> wrote:
Em Ter, 2006-04-11 Ã*s 07:17 -0700, Aahz escreveu:

Can, yes. But should it? The whole point of adding the () option to
classes was to ease the learning process for newbies who don't
understand why classes have a different syntax from functions. Having

class C(): pass

behave differently from

class C: pass

would be of no benefit for that purpose.


Why should a newbie use an old-style class?


Because that's the default. Because lots of existing code still uses
classic classes, so you need to learn them anyway. Because you can't use
new-style classes in code intended for 2.1 or earlier; because of the
changes made in 2.3, I don't particularly recommend new-style classes for
2.2. Because even the second edition of _Learning Python_ (targeted at
Python 2.3) doesn't cover new-style classes much, so I'm certainly not
alone in believing that new-style classes are better avoided for newbies.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"
Apr 11 '06 #21
Wildemar Wildenburger a écrit :
Aahz wrote:
Classic classes are *NOT* deprecated.


I'm surprised ...
So there will be two (in most cases subtly) different classes of classes
(so to speak) for all eternity?

Why is that? Do classic classes have some advantage over new style ones?
If so, what are they?


I'm afraid the only advantage is that they save you a few keystrokes !-)
Apr 11 '06 #22
Aahz a écrit :
In article <44***********************@news.free.fr>,
bruno at modulix <on***@xiludom.gro> wrote:
Please repeat this 101 times each morning:
"thou shall not use old-style classes for they are deprecated".

Classic classes are *NOT* deprecated.


Perhaps not *officially* yet...
Apr 11 '06 #23
In article <44***********************@news.free.fr>,
Bruno Desthuilliers <bd*****************@free.quelquepart.fr> wrote:
Aahz a écrit :
In article <44***********************@news.free.fr>,
bruno at modulix <on***@xiludom.gro> wrote:

Please repeat this 101 times each morning:
"thou shall not use old-style classes for they are deprecated".


Classic classes are *NOT* deprecated.


Perhaps not *officially* yet...


Not even unofficially. The point at which we have deprecation is when
PEP8 gets changed to say that new-style classes are required for
contributions.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"
Apr 11 '06 #24
"Aahz" wrote...
Bruno Desthuilliers wrote:
Aahz a écrit : [...]
Please repeat this 101 times each morning:
"thou shall not use old-style classes for they are deprecated".
Classic classes are *NOT* deprecated.

Perhaps not *officially* yet...


Not even unofficially. The point at which we have deprecation is when
PEP8 gets changed to say that new-style classes are required for
contributions.


My question: Could the old classes be treated in
a new Python treated as new classes with "implicit"
base object? (I know the Zen... ;-)

Example: I use usually a very simple classes. When I add
"(object)" to my class definitions, the code continues to
works fine -- plus I have new features to use.
Why this cannot be done automatically? What could
be broken in the old code if it was threated so?

Thanks for explanation,
pepr
Apr 13 '06 #25
Petr Prikryl wrote:
My question: Could the old classes be treated in
a new Python treated as new classes with "implicit"
base object? (I know the Zen... ;-)

Example: I use usually a very simple classes. When I add
"(object)" to my class definitions, the code continues to
works fine -- plus I have new features to use.
Why this cannot be done automatically? What could
be broken in the old code if it was threated so?

Thanks for explanation,
pepr

Well, apparently this is exactly whats going to happen in the future,
but not until Python 3.0.
Simple as that.

But I guess I'll just keep adding the (object) appendix even after that,
so all my class definitions get syntax highlighting. Also it clarifies
"what the score is" (to paraphrase Bender).

wildemar
Apr 13 '06 #26
Petr Prikryl wrote:
"Aahz" wrote...
Bruno Desthuilliers wrote:
Aahz a �crit :
[...]
Please repeat this 101 times each morning:
>"thou shall not use old-style classes for they are deprecated".

Classic classes are *NOT* deprecated.

Perhaps not *officially* yet...


Not even unofficially. The point at which we have deprecation is when
PEP8 gets changed to say that new-style classes are required for
contributions.


My question: Could the old classes be treated in
a new Python treated as new classes with "implicit"
base object? (I know the Zen... ;-)

Example: I use usually a very simple classes. When I add
"(object)" to my class definitions, the code continues to
works fine -- plus I have new features to use.
Why this cannot be done automatically? What could
be broken in the old code if it was threated so?


Method resolution order in class hierarchies that have multiple inheritance.

--
Robert Kern
ro*********@gmail.com

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Apr 13 '06 #27
Aahz a écrit :
In article <ma***************************************@python. org>,
Felipe Almeida Lessa <fe**********@gmail.com> wrote:
Em Ter, 2006-04-11 Ã s 07:17 -0700, Aahz escreveu:
Can, yes. But should it? The whole point of adding the () option to
classes was to ease the learning process for newbies who don't
understand why classes have a different syntax from functions. Having

class C(): pass

behave differently from

class C: pass

would be of no benefit for that purpose.


Why should a newbie use an old-style class?

Because that's the default. Because lots of existing code still uses
classic classes, so you need to learn them anyway. Because you can't use
new-style classes in code intended for 2.1 or earlier; because of the
changes made in 2.3, I don't particularly recommend new-style classes for
2.2. Because even the second edition of _Learning Python_ (targeted at
Python 2.3) doesn't cover new-style classes much, so I'm certainly not
alone in believing that new-style classes are better avoided for newbies.


Well, old-style classes are perfect to confuse the newbie. After all,
there's nothing like adding a "property" in a class and wondering why
it does not work as expected.

What would be a language without horrible pitfalls ? Newbies would have
it easy and they have no right for an easy language !
Apr 14 '06 #28
Wildemar Wildenburger wrote:
But I guess I'll just keep adding the (object) appendix even after that,
so all my class definitions get syntax highlighting.


why are you using an IDE that doesn't understand Python syntax ?

</F>

Apr 15 '06 #29
In article <e1**********@news.contactel.cz>,
Petr Prikryl <pr*****@skil.cz> wrote:
"Aahz" wrote...
Bruno Desthuilliers wrote:
Aahz a écrit :

Classic classes are *NOT* deprecated.

Perhaps not *officially* yet...


Not even unofficially. The point at which we have deprecation is when
PEP8 gets changed to say that new-style classes are required for
contributions.


My question: Could the old classes be treated in a new Python treated
as new classes with "implicit" base object? (I know the Zen... ;-)

Example: I use usually a very simple classes. When I add "(object)" to
my class definitions, the code continues to works fine -- plus I have
new features to use. Why this cannot be done automatically? What could
be broken in the old code if it was threated so?


Method resolution order is the primary up-front difference, but
introspective code can also have problems. If you're tired of adding
"(object)", put

__metaclass__ = type

at the top of your modules.
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"
Apr 17 '06 #30
Aahz <aa**@pythoncraft.com> wrote:
...
Example: I use usually a very simple classes. When I add "(object)" to
my class definitions, the code continues to works fine -- plus I have
new features to use. Why this cannot be done automatically? What could
be broken in the old code if it was threated so?


Method resolution order is the primary up-front difference, but
introspective code can also have problems. If you're tired of adding


The crucial difference between the old-style classes and the new ones is
about how Python lookups special methods. On an instance of an
old-style class, the lookup is on the instance itself:
class old: pass .... o=old()
o.__str__=lambda:'zap'
print o zap

while on new-style classes, the lookup is ONLY on the class:
class new(object): pass .... n=new()
n.__str__=lambda:'zap'
print n

<__main__.new object at 0x51a4b0>
The change is in fact a very important improvement, but it badly breaks
backwards compatibility, which explains why it can't be applied
automatically (within the 2.* line).
Alex
Apr 17 '06 #31
In article <1h***************************@yahoo.com>,
Alex Martelli <al*****@yahoo.com> wrote:
Aahz <aa**@pythoncraft.com> wrote:

Method resolution order is the primary up-front difference, but
introspective code can also have problems.


The crucial difference between the old-style classes and the new ones
is about how Python lookups special methods.


Right. Thanks! I don't actually use new-style classes much myself, so
I forgot about that one. (And I needed it for the draft that's due
today...)
--
Aahz (aa**@pythoncraft.com) <*> http://www.pythoncraft.com/

"LL YR VWL R BLNG T S"
Apr 17 '06 #32
Aahz <aa**@pythoncraft.com> wrote:
In article <1h***************************@yahoo.com>,
Alex Martelli <al*****@yahoo.com> wrote:
Aahz <aa**@pythoncraft.com> wrote:

Method resolution order is the primary up-front difference, but
introspective code can also have problems.


The crucial difference between the old-style classes and the new ones
is about how Python lookups special methods.


Right. Thanks! I don't actually use new-style classes much myself, so
I forgot about that one. (And I needed it for the draft that's due
today...)


Glad to have been of help, then! BTW, I use new-style classes almost
exclusively (except when maintaining legacy code, or, up to 2.4, for
exceptions)...
Alex
Apr 18 '06 #33
Fredrik Lundh wrote:
why are you using an IDE that doesn't understand Python syntax ?

</F>


Because I don't like using a whole new editor for every new language I'm
using (as you would not use a new hammer for each nail) and jedit just
plain rocks. I guess it could be made to recognize class statements ...
but its python mode works well enough, so I'll rather stick to that and
create python magic.

ok, on to the next useless post ... ;)
wildemar
Apr 18 '06 #34
Ant
(OT) I don't have the same issue with Syntax highlighting, and I use
jEdit as my main Python editor (Though some of the dedicated Python
IDE's have some nice refactoring/code completion stuff, none has the
raw editing power of jEdit).

I'm using jEdit 4.3 pre3 - though I don't recall any problems with 4.2
(and it was only a week ago I changed). The Jython plugin enables
Structure browsing if you haven't already got it installed.

Apr 18 '06 #35
Ant wrote:
(OT) I don't have the same issue with Syntax highlighting, and I use
jEdit as my main Python editor (Though some of the dedicated Python
IDE's have some nice refactoring/code completion stuff, none has the
raw editing power of jEdit).
Could be I got a customized python.xml file when I started ... maybe
I'll have a look.
I'm using jEdit 4.3 pre3 - though I don't recall any problems with 4.2
(and it was only a week ago I changed). I'm cautious on the pre's ... how much of an improvement is it (feature-
and otherwise)?
The Jython plugin enables
Structure browsing if you haven't already got it installed

Really? I got it with the JPyDebug ... but perhaps that includes Jython.
I'll be looking into Jython soon, as I could use a plugin (for AGAST)
and I'm not learning Java just for that!

wildemar
Apr 19 '06 #36
On Tue, 11 Apr 2006 16:30:10 +0200, bruno at modulix <on***@xiludom.gro> wrote:
looping wrote:
bruno at modulix wrote:
looping wrote:

Peter Hansen wrote:
>Georg Brandl wrote:
>
>
>>class C():
>>
>>is meant to be synonymous with is meant by whom, and according to what rationale?

>>class C:
>>
>>and therefore cannot create a new-style class.
>
>I think "looping" understands that, but is basically asking why anyone
>is bothering with a change that involves a part of the language that is
>effectively deprecated. In other words, class(): never used to be
>valid, so why make it valid now? Because it it consistent with passing an empty bases-tuple to type,
which is where a non-empty bases-tuple goes. (See more on metaclass logic below).

>-Peter
Exact.
But I think that if we make "class C():" a synonym of "class
C(object):", it will save lot of keystrokes ;-)
+1
Since the class statement without superclass actually creates an
old-style class, I'd expect the "class MyClass():" variant to behave
the same. Sacrifying readability and expliciteness just to save half a
dozen keystrokes is not pythonic IMHO.
I don't think readability suffer


It does. The statement "class X():" imply there's no superclass, so it
definitiveley should behave the same as "class X:".

I'm not sure what you mean by "no superclass," comparing
class X: pass ... x = X()
type(x).mro() [<type 'instance'>, <type 'object'>]

with
class Y(object): pass ... y = Y()
type(y).mro() [<class '__main__.Y'>, <type 'object'>]
The way I look at it, in new-style-capable Python,

class X: pass

is really effectively sugar for

class X: __metaclass__ = types.ClassType

and
class X(bases): pass

is sugar for

class X(bases): __metaclass__ = type

so
class X(): pass

ought to be sugar for

class X(): __metaclass__ = type

I.e., "old-style classes" really inherit methods from a metaclass that overrides
the methods of object, so that new-style descriptor logic can be tweaked to provide the
old behaviors for classes not having type as metaclass.
and expliciteness could sometimes be
sacrified to simplify the life of developer: ex "abcd"[0:3] ->
"abcd"[:3].


Here there's no ambiguity.
And for newbies, the somewhat magic behavior of the "object" superclass
is not so clear even that it is very explicit.


There's no magic involved here. And I really doubt that having
inconsistant behaviour for "class X():" wrt/ "class X:" will help here.

Again, IMO any bases-tuple including empty should be sugar for __metaclass__ = type, i.e.,

class X(): pass

should be consistent with the empty parens in

X = type("X",(),{}),

not with the backwards-compatibility cleverness (really) of effectively
switching default metaclass to

X = types.ClassType("X",(),{})

using an empty and valid base class tuple as a flag for the switcheroo. Note that the code
erroneously creates an empty tuple for class X:pass, as if it were class X():pass.

(IMO the proper way to indicate the you don't have a tuple is to use None or some other sentinel,
not abuse a perfectly legal tuple value).
dis.dis(compile('class X:pass','','exec')) 1 0 LOAD_CONST 0 ('X') <<--+-- ought to be LOAD_CONST 0 (None)
3 BUILD_TUPLE 0 <<--'
6 LOAD_CONST 1 (<code object X at 02EE7EA0, file "", line 1>)
9 MAKE_FUNCTION 0
12 CALL_FUNCTION 0
15 BUILD_CLASS
16 STORE_NAME 0 (X)
19 LOAD_CONST 2 (None)
22 RETURN_VALUE

vs code for class x(something):pass
dis.dis(compile('class X(object):pass','','exec'))
1 0 LOAD_CONST 0 ('X')
3 LOAD_NAME 0 (object)
6 BUILD_TUPLE 1
9 LOAD_CONST 1 (<code object X at 02EFB9A0, file "", line 1>)
12 MAKE_FUNCTION 0
15 CALL_FUNCTION 0
18 BUILD_CLASS
19 STORE_NAME 1 (X)
22 LOAD_CONST 2 (None)
25 RETURN_VALUE

IMO generating an empty tuple for class X():pass is a natural variation of the immediately above.
What is un-natural is using an empty tuple as a logical flag for old-style classes (implementing
the latter by selecting types.ClassType as the metaclass instead of type)
for what would othewise work perfectly normally with the default metaclass of type.

Code generation would need to provide something other than an empty tuple
on the stack for class X:pass (IWT None would work?) as the logic flag for old-style classes,
and build_class in ceval.c would have to be changed to recognize
the new flag value (None?) for calling types.ClassType,
and pass tuples (including empty) through to type.
When I write script I don't use new-style class


You should.
cause is bother me to
type "(object)" when I don't need their features.

so for now put

__metaclass__ = type

once at the top of your module source, and all your

class X:
...

will be interpreted as

class X:
__metaclass__ = type
...

instead of implicitly as

class X:
__metaclass__ = types.ClassType
...

Please repeat this 101 times each morning:
"thou shall not use old-style classes for they are deprecated".

(snip)
So this new syntax is a good way to boost their uses without bother
with compatibility of existing code IMHO.


It's mostly a good way to add inconsistency and confusion to a situation
that's already confusing enough for newbies.

I don't agree with your idea of inconsistency.

IMO it would be better to explain that a legal basetuple value (empty tuple) is currently
being abused as a logical flag to call types.ClassType(clsname, basestuple, clsdict)
instead of type(clsname, basestuple, clsdict), and explain that it will be corrected, so that
class X():pass will now call the latter, consistent with class X(bases):pass.

Bottom line: IMO class C():pass should create a new-style class,
and the parens serve well as a reminder of which kind it is, whether empty or not,
until py3k.

I.e., make it easy for newbies: parens means new-style, no parens means old-style, until py3k.

Pontificating pushes my counter-pontificating button; that's the only explanation
I have for doing this. I was going to stop wasting time, but find myself
unable as yet fully to abandon scanning clp and python-dev ;-/

Regards,
Bengt Richter
Apr 23 '06 #37
Christophe wrote:
Aahz a écrit :
I'm certainly not alone in believing that new-style classes are
better avoided for newbies.
Well, old-style classes are perfect to confuse the newbie. After all,
there's nothing like adding a "property" in a class and wondering why
it does not work as expected.


Well, for years everyone joked about Java's "public static void main"
business - confusing boilerplate for newbies, they said - but apart
from various deep runtime-dependent arguments, I can't see how "class
something(object)" is any less forgivable, especially in a teaching
environment where you've just explained that "object" and "instance"
are mostly interchangeable terms for something made from a class.

And to make the "guard of shame" (the opposite of a guard of honour)
complete in this sorry tale, it should be pointed out that they don't
make you write "class Something extends Object" all over the place in
Java, or at least not when I last wrote any Java code. Something to
think about for Python 3000 in the context of reaching a wider
audience, I suppose.
What would be a language without horrible pitfalls ? Newbies would have
it easy and they have no right for an easy language !


;-) Or were you being serious? :-O

Paul

Apr 24 '06 #38
On Sun, 23 Apr 2006 22:12:01 GMT, bo**@oz.net (Bengt Richter) wrote:
[...]
(IMO the proper way to indicate the you don't have a tuple is to use None or some other sentinel,
not abuse a perfectly legal tuple value).
dis.dis(compile('class X:pass','','exec')) 1 0 LOAD_CONST 0 ('X') <<--+-- ought to be LOAD_CONST 0 (None) Oops, leave the 'X' of course, just replace the next line 3 BUILD_TUPLE 0 <<--'
6 LOAD_CONST 1 (<code object X at 02EE7EA0, file "", line 1>)
9 MAKE_FUNCTION 0
12 CALL_FUNCTION 0
15 BUILD_CLASS
16 STORE_NAME 0 (X)
19 LOAD_CONST 2 (None)
22 RETURN_VALUE

vs code for class x(something):pass
dis.dis(compile('class X(object):pass','','exec'))

1 0 LOAD_CONST 0 ('X')
3 LOAD_NAME 0 (object)
6 BUILD_TUPLE 1
9 LOAD_CONST 1 (<code object X at 02EFB9A0, file "", line 1>)
12 MAKE_FUNCTION 0
15 CALL_FUNCTION 0
18 BUILD_CLASS
19 STORE_NAME 1 (X)
22 LOAD_CONST 2 (None)
25 RETURN_VALUE


Regards,
Bengt Richter
Apr 25 '06 #39

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: beliavsky | last post by:
Ideally, one can use someone's C++ code by just looking at the header files (which should contain comments describing the functions in addition to function definitions), without access to the full...
3
by: Alex Stapleton | last post by:
Whenever I run python I get "Warning! you are running an untested version of Python." prepended to the start of any output on stdout. This is with Debian and python 2.3 (running the debian...
15
by: Jordan Rastrick | last post by:
First, a disclaimer. I am a second year Maths and Computer Science undergraduate, and this is my first time ever on Usenet (I guess I'm part of the http generation). On top of that, I have been...
7
by: jeffbernstein | last post by:
Greetings. I'm reading "How to think like a computer scientist: Learning with Python" and there's a question regarding string operations. The question is, "Can you think of a property that...
8
by: Mike Meng | last post by:
Hi all, I just finished reading Learning Python 3rd ed, and am doing my first Python application, which retrieves and process text and XML documents from Web. Python helped me to write the...
5
by: John Nagle | last post by:
This bug, " robotparser interactively prompts for username and password", has been open since 2003. It killed a big batch job of ours last night. Module "robotparser" naively uses "urlopen" to...
14
by: Ivan Voras | last post by:
Hi, I'm looking for a construct that's similar to (Turbo) Pascal's "with" statement. I read about the Python's new "with" statement, but I was dissapointed to learn that it does something...
41
by: none | last post by:
Hello, IIRC, I once saw an explanation how Python doesn't have "variables" in the sense that, say, C does, and instead has bindings from names to objects. Does anyone have a link? Thanks, ...
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
8
by: Astan Chee | last post by:
Hi, Thanks for all the help from the previous problem. Turns out I didnt have to use wxSizers, just change the size of my wxWidgets everytime a EVT_SIZE is called. Anyway, Im trying to find a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.