Guido's new method definition idea

  • Thread starter Hendrik van Rooyen
  • Start date
R

Russ P.

Allowing "$" as a substitute for "self" wouldn't require this new syntax.
class C:
    def method($, arg):
        $.value = arg
I'm strongly against this. This looks ugly and reminds me of Perl and
Ruby. (I don't have anything against these languages, but there's a
reason I use Python).
Russ P. wrote:
On Dec 5, 6:21 pm, "Daniel Fetchinson" <[email protected]>
wrote:
Hi folks,
The story of the explicit self in method definitions has been
discussed to death and we all know it will stay. However, Guido
himself acknowledged that an alternative syntax makes perfect sense
and having both (old and new) in a future version of python is a
possibility since it maintains backward compatibility. The alternative
syntax will be syntactic sugar for the old one. This blog post of his
is what I'm talking about:
http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay...
The proposal is to allow this:
class C:
    def self.method( arg ):
        self.value = arg
        return self.value
instead of this:
class C:
    def method( self, arg ):
        self.value = arg
        return self.value
I.e. explicit self stays only the syntax is slightly different and may
seem attractive to some. As pointed out by Guido classmethods would
work similarly:
class C:
    @classmethod
    def cls.method( arg ):
        cls.val = arg
        return cls.val
The fact that Guido says,
"Now, I'm not saying that I like this better than the status quo. But
I like it a lot better than [...] but it has the great advantage that
it is backward compatible, and can be evolved into a PEP with a
reference implementation without too much effort."
shows that the proposal is viable.
I'd like this new way of defining methods, what do you guys think?
Anyone ready for writing a PEP?
Cheers,
Daniel
--
Psss, psss, put it down! -http://www.cafepress.com/putitdown
I like it.
I'll even go a step further and suggest that "$" be allowed as a
substitute for "self". It looks like a capital "S" (for Self), and it
stands out clearly. It also makes code more succinct with no loss of
readability. Think of the line wraps that could be avoided.
It looks "ugly" simply because it is new to you. Once you get used to
it, I'll bet it will look fine. And resemblance to another language is
not a very good reason to reject it.

Perl is not new to me and I am familiar with the syntax, such as it
is.  I find it unspeakably ugly.  So, no, you would lose your bet if
it were me.

Carl Banks

I don't know much about Perl, but my understanding is that a dollar
sign must be used every time a variable is dereferenced, as in bash or
other shell languages. What we are proposing here is something
entirely different: the dollar sign would simply be a shorthand for
"self". In Perl, the dollar sign is clutter, but in this case it
actually reduces clutter.

Python already uses shorthand extensively. How about "def"? For people
who are so worried about self-explanatory symbols, what the heck does
that stand for? Default? Defeat? Defect? Defunct? Defer?

At some time in the past, a conscious decision was made to save three
characters in the word "define" by abbreviating it as "def". The
suggestion to abbreviate "self" as "$" also saves three characters.
And "self" appears much more often than "def", so an abbreviation is
equally or more justified in my opinion.
 
A

Andreas Waldenburger

I don't know much about Perl, but my understanding is that a dollar
sign must be used every time a variable is dereferenced, as in bash or
other shell languages. What we are proposing here is something
entirely different: the dollar sign would simply be a shorthand for
"self". In Perl, the dollar sign is clutter, but in this case it
actually reduces clutter.
This is the weirdest thing. Not a month seems to go by that this old
hat isn't dug up again. You're fighting windmills. Why do you think
self has been around so long? Because it's the best choice? No, because
it works and there just is no need to change it (where "need" is
defined by the desires of the majority).

Getting the Python comunity to replace self with something shorter will
never compensate for the time you spent bullying it through. I'd much
rather spend time coding than complaining, but that might be just me.
(BTW: I have my editor set up so that "s" is expanded to "self" and "."
is expanded to "self."; I virtually lose no time at all with self and
would gain nothing from a change to it.

And regarding line-lengths: My code lines tend to be shorter than my
comments explaining them, but that may again be just me.


Excuse my being stubborn. :)
/W
 
S

Steven D'Aprano

Did you get two votes in the Presidential election too? 8^)

You know, occasionally you stumble across people on the Internet who
aren't from the USA. Some of us even speak English almost as good as
native speakers *wink*

In any case, in this context -2 refers to the strength of feeling, not
the number of votes. Python is not a democracy, and our Beloved BDFL
Guido graciously takes our needs and wants into account before making
decisions.
 
C

Carl Banks

Allowing "$" as a substitute for "self" wouldn't require this new syntax.
class C:
    def method($, arg):
        $.value = arg
I'm strongly against this. This looks ugly and reminds me of Perl and
Ruby. (I don't have anything against these languages, but there's a
reason I use Python).
Russ P. wrote:
On Dec 5, 6:21 pm, "Daniel Fetchinson" <[email protected]>
wrote:
Hi folks,
The story of the explicit self in method definitions has been
discussed to death and we all know it will stay. However, Guido
himself acknowledged that an alternative syntax makes perfect sense
and having both (old and new) in a future version of python is a
possibility since it maintains backward compatibility. The alternative
syntax will be syntactic sugar for the old one. This blog post of his
is what I'm talking about:
http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay...
The proposal is to allow this:
class C:
    def self.method( arg ):
        self.value = arg
        return self.value
instead of this:
class C:
    def method( self, arg ):
        self.value = arg
        return self.value
I.e. explicit self stays only the syntax is slightly different and may
seem attractive to some. As pointed out by Guido classmethods would
work similarly:
class C:
    @classmethod
    def cls.method( arg ):
        cls.val = arg
        return cls.val
The fact that Guido says,
"Now, I'm not saying that I like this better than the status quo.. But
I like it a lot better than [...] but it has the great advantage that
it is backward compatible, and can be evolved into a PEP with a
reference implementation without too much effort."
shows that the proposal is viable.
I'd like this new way of defining methods, what do you guys think?
Anyone ready for writing a PEP?
Cheers,
Daniel
--
Psss, psss, put it down! -http://www.cafepress.com/putitdown
I like it.
I'll even go a step further and suggest that "$" be allowed as a
substitute for "self". It looks like a capital "S" (for Self), and it
stands out clearly. It also makes code more succinct with no loss of
readability. Think of the line wraps that could be avoided.
It looks "ugly" simply because it is new to you. Once you get used to
it, I'll bet it will look fine. And resemblance to another language is
not a very good reason to reject it.
Perl is not new to me and I am familiar with the syntax, such as it
is.  I find it unspeakably ugly.  So, no, you would lose your bet if
it were me.
Carl Banks

I don't know much about Perl, but my understanding is that a dollar
sign must be used every time a variable is dereferenced, as in bash or
other shell languages. What we are proposing here is something
entirely different: the dollar sign would simply be a shorthand for
"self". In Perl, the dollar sign is clutter, but in this case it
actually reduces clutter.

But it's ugly. No amount of rationalization will make it not ugly.

Python already uses shorthand extensively. How about "def"? For people
who are so worried about self-explanatory symbols, what the heck does
that stand for? Default? Defeat? Defect? Defunct? Defer?

At some time in the past, a conscious decision was made to save three
characters in the word "define" by abbreviating it as "def". The
suggestion to abbreviate "self" as "$" also saves three characters.
And "self" appears much more often than "def", so an abbreviation is
equally or more justified in my opinion.

def isn't ugly.


Carl Banks
 
B

bearophileHUGS

Erik Max Francis:
your precise proposal has
been brought up countless times on comp.lang.python

And something tells me that it will keep coming up many more times in
the following years too.

Bye,
bearophile
 
L

Lie

What would be interesting would be some syntactical sugar to get rid of
the 'self' (at least in the code body).

example:
class C:
    class_elements a,b,c,d

    def method(self,arg):
        global d
        a,b,c = arg[0..3]
        d = a + b
        self.e = a + d

Nah, that would make it not explicit. Explicit here also means that to
refer to self's a, we need to explicitly refer to self.
 
L

Lie

Hi folks,

The story of the explicit self in method definitions has been
discussed to death and we all know it will stay. However, Guido
himself acknowledged that an alternative syntax makes perfect sense
and having both (old and new) in a future version of python is a
possibility since it maintains backward compatibility. The alternative
syntax will be syntactic sugar for the old one. This blog post of his
is what I'm talking about:

http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay...

The proposal is to allow this:

class C:
    def self.method( arg ):
        self.value = arg
        return self.value

instead of this:

class C:
    def method( self, arg ):
        self.value = arg
        return self.value

I.e. explicit self stays only the syntax is slightly different and may
seem attractive to some. As pointed out by Guido classmethods would
work similarly:

class C:
    @classmethod
    def cls.method( arg ):
        cls.val = arg
        return cls.val

To sum up:

Arguments on Status Quo:
+ Andreas Waldenburger: it works and there just is no need to change
it
+ need no change
+ Andreas Waldenburger: Getting the Python comunity to replace self
with something shorter will never compensate for the time you spent
bullying it through
- Confusion for number of parameters
- The standard 'self' is too long
- Newbie FAQ
- It is ugly

Arguments on "def self.method(" as "def method(self" inside a class:
+ OP: It reduces confusion for number of parameters
+ Patrick Mullen: The symetry of "def self.func(blah)==def func
(self,blah)" and "ob.func(blah)==func(ob.blah)" is kind of neat.
+ OP: Backward compatible
+ OP: It is explicit
- Marc 'Blackjack' Rintsch: they [newcomers] will run into *both*
variants in tutorials, code, and books, so it might be even more
confusing.
- Carl Banks: def <something>(): == <somthing> = <the defined
function>, but def self.method(): return 1 != self.method = lambda: 1
- `self` in this context might be misconstrued as the class object and
thus `def self.foo` might be misunderstood ... as a defining a
classmethod rather than an instance method.
- It is ugly
? Syntax sugar or replacement? Many people prefers this to be
replacement to reduce confusion.

Arguments on variants of $
+ Russ P.: looks like S(elf)
+ Russ P.: more succinct with no loss of readability
- Antoine de Groote: slightly less readable.
- Antoine de Groote: wouldn't require this new syntax (def meth($,
args): $.foo)
- Andreas Waldenburger: "self" is a speaking identifier, "$" isn't
- Obscure symbol
- It is ugly

Unresolved:
? Patrick Mullen: Outside a class definition?

I think we have to test this on newbies. Personally, I think the new
syntax would confuse newbies too, though not as much as the tricky
error message we currently have (code: foo.bar(1, 2, 3, 4); Error:
TypeError: foo() takes exactly 4 arguments (5 given); Newbie: "what
the... 1.. 2.. 3.. 4.., I correctly gave 4 arguments, python counted
the wrong number of arguments").

If this dead horse is revived because of that reason, then I'd go with
changing the error message to something that is less confusing to
newbies[1]. I remember being tripped with the (thinking that python
miscounted the number of argument) when I was new. This has the
advantage of backward compatibility and no syntax change, just less
misleading error message.

[1] anything could work, but I like this one: (c is an instance of
class C)
if the code is: c.foo(...), Error: "TypeError: c.foo() takes exactly 3
argument"
while if the code is: C.foo(...), Error: "C.foo() takes exactly 4
arguments"
You can implement c.foo as a curried C.foo function, catch C.foo's
TypeError exception then reraise it as c.foo exception.
 
N

News123

Sorry Dennis,


I don't understand your answer.
I'm not very knowledgable with all the OO vocabulary, but just use OO.

self.a , self.b , self.c are stored in the object and could later be
used by other object-methods.

like
def print_a_b_c(self):
print self,a,self.b,self.c


the name 'class_elements' was just a suggestion it could be also
something like 'auto_prepend_self' or whatever.

bye


N

example:
class C:
class_elements a,b,c,d

def method(self,arg):
global d
a,b,c = arg[0..3]
d = a + b
self.e = a + d

instead of
class C:
def method(self,arg):
self.a,self.b,self.c,self.d = arg[0..4]
self.e = self.a + self.b
I would declare this a poor example, since a, b, c, aren't used as
attributes -- they are just invocation locals.
 
N

News123

Lie said:
What would be interesting would be some syntactical sugar to get rid of
the 'self' (at least in the code body).

example:
class C:
class_elements a,b,c,d

def method(self,arg):
global d
a,b,c = arg[0..3]
d = a + b
self.e = a + d

Nah, that would make it not explicit. Explicit here also means that to
refer to self's a, we need to explicitly refer to self.

Well being explicit when trying to suggest an implicit syntax (in order
to reduce typing) is a little difficult ;-)

Though you're right my main goal is not being implicit but would be
reducing typing and have shorter source code lines.

If 'global '<varname>' is accepted inside a def, then moving
'class_elements <varnames>' inside the def could be acceptable as well
though it would requiere, that this statement is repeated per def

bye


N
 
S

Steven D'Aprano

Sorry Dennis,


I don't understand your answer.
I'm not very knowledgable with all the OO vocabulary, but just use OO.

self.a , self.b , self.c are stored in the object and could later be
used by other object-methods.

In Python terminology, they are called "attributes". This is fairly
standard for most OO languages too.

If the attribute is stored in the instance, it is an "instance
attribute". If it is shared by all instances and stored in the class, it
is a "class attribute".
 
A

Arnaud Delobelle

[about removing self]
P.S. You're beating a long-dead horse here; your precise proposal has
been brought up countless times on comp.lang.python and shot down
every single time for the same reason. It isn't going to happen.

I guess it's part of the process of learning Python :)
 
A

Andreas Waldenburger

I'm not sure though whether allowing both syntaxes would make things
more or less confusing. It might actually be helpful in some respects
for newcomers to realize that self.method(arg) is somewhat the same
as method(self, arg). [snip]

A professor of mine once said something to the effect that in teaching
it is not so important to ­*tell* people a lot but to *omit* as much as
practically possible. This might be the single most wise thing I have
ever heard.

If you're a newbee and you know nothing of the language, and probably
nothing about programming at all, having two possibilities will
thoroughly confuse you. Only when you have a working(!) knowledge of
one version will you be able to appreciate the other. Since in this
case the other option has no practical benefits whatsoever (IMHO),
there's absolutely no need for it.


/W
PS: I will state again that I do like the idea in itself, its just about
20 years too late for it.
 
A

Andreas Waldenburger

I think we have to test this on newbies. [snip]
Now that's talking like a programmer!

Ideas on how such a survey could be conducted? Anyone?

If this dead horse is revived because of that reason, then I'd go with
changing the error message to something that is less confusing to
newbies[1].
+ googol

I remember being tripped with the (thinking that python
miscounted the number of argument) when I was new. This has the
advantage of backward compatibility and no syntax change, just less
misleading error message.

[1] anything could work, but I like this one: (c is an instance of
class C)
if the code is: c.foo(...), Error: "TypeError: c.foo() takes exactly 3
argument"
while if the code is: C.foo(...), Error: "C.foo() takes exactly 4
arguments"
You can implement c.foo as a curried C.foo function, catch C.foo's
TypeError exception then reraise it as c.foo exception.
I'm not sure that I'd find that less confusing. Because a c.foo()
*does* take four arguments, not three. It's just that the first one is
implicit (Right?).


How about:

"TypeError: c.foo() takes exactly 3 arguments in addition to
the implicit instance reference."

or

"TypeError: c.foo() takes exactly 4 arguments (5 given, including the
implicit instance reference)"

.... or something less kludgy in that general direction. This would
explain exactly what is wrong.

/W
 
B

Bruno Desthuilliers

Daniel Fetchinson a écrit :
(snip)
It doesn't add anything but makes something that exists a bit clearer

Err... I fail to see how magically transforming def self.foo(...) into
def foo(self, ...) makes anything clearer about what really happens and
how Python's object model works.
and friendlier to newbies.

I'd rather say "more acceptable to java-brainwashed developpers".
 
B

Bruno Desthuilliers

News123 a écrit :
This has been debated to hell and back. And it's *not* going to happen.
example:
class C:
class_elements a,b,c,d

def method(self,arg):
global d
a,b,c = arg[0..3]
d = a + b
self.e = a + d
Nah, that would make it not explicit. Explicit here also means that to
refer to self's a, we need to explicitly refer to self.

Well being explicit when trying to suggest an implicit syntax (in order
to reduce typing) is a little difficult ;-)

Though you're right my main goal is not being implicit but would be
reducing typing and have shorter source code lines.

then use 's' instead of 'self'.
If 'global '<varname>' is accepted inside a def, then moving
'class_elements <varnames>' inside the def could be acceptable as well

self.x is an instance attribute, not a class attribute. Aslo, the def
statement creates a function, not a method, so the notion of
"class_element" or however you name it is totally irrelevant here.
 
D

Daniel Fetchinson

Hi folks,
The story of the explicit self in method definitions has been
discussed to death and we all know it will stay. However, Guido
himself acknowledged that an alternative syntax makes perfect sense
and having both (old and new) in a future version of python is a
possibility since it maintains backward compatibility. The alternative
syntax will be syntactic sugar for the old one. This blog post of his
is what I'm talking about:

http://neopythonic.blogspot.com/2008/10/why-explicit-self-has-to-stay...

The proposal is to allow this:

class C:
def self.method( arg ):
self.value = arg
return self.value

instead of this:

class C:
def method( self, arg ):
self.value = arg
return self.value



-1

I explained why deep in the thread but I'll elaborate more here. When
I see a def statement, I mentally equate that to an assigment to the
thing being def'ed. So for instance, when I see this:

def <something>():

I think of it like this:

<somthing> = <the defined function>


Thus, if I were to see a function definition like this

def foo.bar(): return 1

I would think you were defining a function and assigning it to
foo.bar. IOW, it would be mostly equivalent to this:

foo.bar = lambda: 1


(Analogously, I would expect a definition like this:

def baz[10](): return 1

to be equivalent to this:

baz[10] = lambda: 1 )


So, if, inside a class definition, I were to see this:

def self.method(): return 1

Well, I'd understand that is was a method assigment, of course, but it
would conflict with what I would expect the natural meaning of
something like def a.b() would be. The above statement is not
equivalent to:

self.method = lambda: 1

but I think that's what it ought to be, in general.

Similarly, to those coming from Ruby or those operating under the
frequent misunderstanding that the `def`s are happening in the context
of a class object (which in reality has yet to be created), `self` in
this context might be misconstrued as the class object and thus `def
self.foo` might be misunderstood (through the intuitive equivalence
you mention) as a defining a classmethod rather than an instance
method.

This is actually a real counter argument, I think. Self, the instance,
doesn't exist until it is created and certainly doesn't exist during
class creation. So something like

class C:
def self.meth( arg ):
return arg

can be confusing since 'self' appears as if it was defined in the
scope of C but clearly it isn't yet.

Cheers,
Daniel
 
A

Andreas Waldenburger

I'd rather say "more acceptable to java-brainwashed developpers".

Why would you rather be unfriendly and seed ambivalence? I do see the
fun in a little Python snobbism, but ... come on.

/W
 
A

Antoine De Groote

Bruno said:
Daniel Fetchinson a écrit :
(snip)

Err... I fail to see how magically transforming def self.foo(...) into
def foo(self, ...) makes anything clearer about what really happens and
how Python's object model works.


I'd rather say "more acceptable to java-brainwashed developpers".

Nicely put! :)
 

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top