By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,172 Members | 762 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,172 IT Pros & Developers. It's quick & easy.

explicit self revisited

P: n/a
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version):

1. Instance variables can be easily distinguished from local variables.

2. A method from a particular class can be called as
baseclass.methodname(self, <argument list>).

3. No need for declarations to disambiguate assignments to local/instance
variables.

All these reasons are valid and retained by the following suggestion: let
self be represented by the dot, e.g. replace

class someTest(unittest.TestCase):
def setUp(self):
self.ly = yList()
self.m1 = self.ly[0].message
self.m2 = self.ly[1].message
self.m3 = self.ly[2].message

def testList(self):
self.assertEqual(len(self.ly),3)
self.assertEqual(self.m1),"Ho")
self.assertEqual(self.m2),"HoHo")
self.assertEqual(self.m3),"HoHoHo")

by

class x(unittest.TestCase):
def .setUp():
.ly = yList()
.m1 = .ly[0].message
.m2 = .ly[1].message
.m3 = .ly[2].message

def .testList():
.assertEqual(len(.ly),3)
.assertEqual(.m1),"Ho")
.assertEqual(.m2),"HoHo")
.assertEqual(.m3),"HoHoHo")

Methods could still be referenced e.g. as x.testList(someInstance).
The current self syntax could still be valid (for backward compatibility.)
Advantages of the new syntax:

1. Enhanced readability, less verbosity

2. Unambiguous: no need to tell newbies that a virtuous pythoneer
has to stick to self instead of abbreviate it as s.

3. One argument less for "Python OO bolted on" propaganda.

The second reason is the most important for me. I consider syntax control
by a code of conduct as lame.

The "leading dot" syntax could have further advantages:

class x(object):
a = "" # static variable
.b = 0 # instance variable

This could replace parameterless __init__ methods. Methods without leading
dots could be considered static without a staticmethod decorator. For
backward compatibility this behaviour should be explicitly activated e.g. by
__autostatic__ = true.

What do you think?

--
Regards/Gruesse,

Peter Maas, Aachen
E-mail 'cGV0ZXIubWFhc0B1dGlsb2cuZGU=\n'.decode('base64')
Nov 11 '06 #1
Share this Question
Share on Google+
24 Replies


P: n/a
Peter Maas wrote:
What do you think?
cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?

</F>

Nov 11 '06 #2

P: n/a
cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?
and before anyone complains; please note that they're working through

http://www.effbot.org/pyfaq/design-index.htm

one article at a time. who's going to be the first one to argue that
Python needs a goto statement ?

</F>

Nov 11 '06 #3

P: n/a

Fredrik Lundh wrote:
Fredrik Lundh wrote:
cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?

and before anyone complains; please note that they're working through

http://www.effbot.org/pyfaq/design-index.htm
That site is a bunch of FUD -
The explicit self is there simply because OOP was tacked onto python as
an afterthought.
Why not just be honest about it. It's too late to change Python's
syntax. It just means a little extra typing. If it really bothers
someone, use "s" instead of "self" or else use Ruby.

Nov 11 '06 #4

P: n/a
Doug (Holton?) wrote:
>and before anyone complains; please note that they're working through

http://www.effbot.org/pyfaq/design-index.htm

That site is a bunch of FUD -
the official FAQ is a bunch of FUD? are you sure you know what FUD means?

</F>

Nov 11 '06 #5

P: n/a

DougThe explicit self is there simply because OOP was tacked onto
Dougpython as an afterthought.

Got a reference to support that claim?

Skip
Nov 11 '06 #6

P: n/a
On Sat, 11 Nov 2006 18:09:59 -0600, skip wrote:
>
DougThe explicit self is there simply because OOP was tacked onto
Dougpython as an afterthought.

Got a reference to support that claim?
Of course not, since it is a classic example of trolling.

By comparison, the way I read the Original Poster, he was sincere if
misguided -- but Doug's bullshit response is a typical attempt to throw
petrol on an already hot situation.
--
Steven.

Nov 12 '06 #7

P: n/a
Dennis Lee Bieber wrote:
>one article at a time. who's going to be the first one to argue that
Python needs a goto statement ?
Especially since there is a comefrom <G>
ah, good point. I've updated the FAQ.

</F>

Nov 12 '06 #8

P: n/a
On Sat, 11 Nov 2006 22:39:37 +0100, Peter Maas wrote:

[snip]
let self be represented by the dot, e.g. replace

class someTest(unittest.TestCase):
def setUp(self):
self.ly = yList()
self.m1 = self.ly[0].message
self.m2 = self.ly[1].message
self.m3 = self.ly[2].message
by

class x(unittest.TestCase):
def .setUp():
.ly = yList()
.m1 = .ly[0].message
.m2 = .ly[1].message
.m3 = .ly[2].message
On the assumption that Peter was sincere, and not trolling, I thought I'd
make a point-by-point response, but then X crashed and took my post with
it. So here's the brief version.

Implicit self will never be used for Python, because it is redundant so
long as there is a need for explicit self, and there is a need for
explicit self because there are a number of basic, dare I say
*fundamental* programming techniques that require an explicit self.

For example, delegation.

class MyClass(Parent):
def method(self, arg):
Parent.method(self, arg)
# what are you going to write? Parent.method(,arg) maybe?

Here's another fundamental technique that implicit self breaks.

class MyList(list):
def __iadd__(self, other):
# in-place addition
other = transform(other) # do something to the argument
super(MyList, self).__iadd__(other) # call the superclass
# could be written .__class__.__iadd__(other)
# BUT be aware the semantics are NOT the same!
return self # and return the suitably modified instance

You can't explicitly return the instance unless you have an explicit name
for it. (And if you are thinking of creating more magic syntax that
implicitly returns self, just don't bother.)

Even more fundamentally, any time you need to pass the instance to a
function, you need an explicit self. (Delegation is a special case of this.)

def __str__(self):
# I like semicolons and dislike square brackets.
return ';'.join(self)

--
Steven.

Nov 12 '06 #9

P: n/a

Fredrik Lundh wrote:
Doug wrote:
>>
Fredrik Lundh wrote:
>>Fredrik Lundh wrote:
cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?
>>and before anyone complains; please note that they're working through
>> http://www.effbot.org/pyfaq/design-index.htm

That site is a bunch of FUD -
The explicit self is there simply because OOP was tacked onto python as
an afterthought.
Why not just be honest about it. It's too late to change Python's
syntax. It just means a little extra typing. If it really bothers
someone, use "s" instead of "self" or else use Ruby.

the official FAQ is a bunch of FUD? are you sure you know what FUD means?

</F>
You idiot. Putting the word "official" in front of something doesn't
mean it can't be FUD. Especially when it is written by people such as
yourself. Have you not paid attention to anything happening in
politics around the world during your lifetime?
And yes, actually, the dash after FUD was where I was going to link to
a definition of FUD to show I really meant to use that term. It
doesn't appear that you believe anything that isn't on your own effbot
site, however.

Nov 12 '06 #10

P: n/a
Dennis Lee Bieber <wl*****@ix.netcom.comwrote:
On Sun, 12 Nov 2006 01:55:35 +0100, Fredrik Lundh
<fr*****@pythonware.comdeclaimed the following in comp.lang.python:

>>
ah, good point. I've updated the FAQ.
Ah, but do we dare update the Wikipedia link to include Python as a
language capable of COMEFROM? <G>
Somebody definitely should. The current wikipedia article contains an
example in a hypothetical dialect of BASIC ('because an actual example in
INTERCAL would be too difficult to read'). Somebody should enhance the
article with the equivalent example in Python which is both easy to read
and can actually be run:

from goto import goto, comefrom, label

comefrom .repeat
name = raw_input('what is your name? ')
if name:
print "Hello",name
label .repeat

print "Goodbye!"

Nov 12 '06 #11

P: n/a
On Sun, 12 Nov 2006 02:14:32 -0500, Doug <aj******@gmail.comwrote:
I was going to link to
a definition of FUD to show I really meant to use that term.
Oooh.
If you had just mentioned your dyslogia,
it would have saved us all some time.
Thanks!
Alan
Nov 12 '06 #12

P: n/a
On Sat, 2006-11-11 at 23:14 -0800, Doug wrote:
Fredrik Lundh wrote:
Doug wrote:
>
Fredrik Lundh wrote:
Fredrik Lundh wrote:
cannot all you clueless trolls who cannot think of a single useful thing
to contribute to Python start your own newsgroup?

and before anyone complains; please note that they're working through

http://www.effbot.org/pyfaq/design-index.htm

That site is a bunch of FUD -
The explicit self is there simply because OOP was tacked onto python as
an afterthought.
Why not just be honest about it. It's too late to change Python's
syntax. It just means a little extra typing. If it really bothers
someone, use "s" instead of "self" or else use Ruby.
the official FAQ is a bunch of FUD? are you sure you know what FUD means?

</F>

You idiot. Putting the word "official" in front of something doesn't
mean it can't be FUD.
Fredrik doesn't have to prove that the official FAQ isn't FUD. You,
Doug, have to prove that it is, because you accused it of being FUD.

By your logic, I can prove that you are a piece of cheddar cheese. I
will simply assert that you are a piece of cheddar cheese. You might
respond by making a surprisingly lucid (for a piece of cheddar) argument
that you are a sentient being. To this I will respond "You idiot,
proving that you are a sentient being doesn't mean you can't be a piece
of cheddar." QED.

Since you are too lazy to post the wikipedia link, please allow me:
http://en.wikipedia.org/wiki/Fear%2C...inty_and_doubt

According to that definition, FUD is "a sales or marketing strategy of
disseminating negative (and vague) information on a competitor's
product."

Even if the FAQ could, by a stretch of the imagination, be considered a
sales or marketing strategy, you'd be hard-pressed to find evidence of
it disseminating negative information on a competitor's product. Hence,
it's not FUD.

-Carsten
Nov 12 '06 #13

P: n/a
Carsten Haese wrote:
According to that definition, FUD is "a sales or marketing strategy of
disseminating negative (and vague) information on a competitor's
product."
Doug "cheddar cheese" Holton (who has a long history of posting
seriously confused and/or abusive stuff under a number of aliases) is
a developer of a "competing" language named Boo.

I suppose that in his view, language advocacy is a zero-sum game, so
positive comments about Python can be considered as FUD against his own
project. He's even invented his own del.icio.us tag for this purpose:

http://del.icio.us/tag/pythonfud

</F>

Nov 12 '06 #14

P: n/a
I suppose that in his view, language advocacy is a zero-sum game, so
positive comments about Python can be considered as FUD against his own
project. He's even invented his own del.icio.us tag for this purpose:

http://del.icio.us/tag/pythonfud
now at:

http://del.icio.us/tag/python-fud

</F>

Nov 12 '06 #15

P: n/a
Peter Maas wrote:
The Python FAQ 1.4.5 gives 3 reasons for explicit self (condensed version):

1. Instance variables can be easily distinguished from local variables.

2. A method from a particular class can be called as
baseclass.methodname(self, <argument list>).

3. No need for declarations to disambiguate assignments to local/instance
variables.

All these reasons are valid and retained by the following suggestion: let
self be represented by the dot <snip>
This suggestion has been discussed in the past (I remember having the
same idea
myself when I first learned Python). But at the end I believe the
explicit 'self' is
a better solution, because there are cases where it is useful to have
it (see Steven
d'Aprano's post) and also I like the fact that I can spell it 'cls' in
classmethods and in
metaclasses. After a while one gets used to it and if you think a bit
it makes quite a
lot sense. Also, having the self explicit, makes Python object system
better (i.e.
it goes more in the direction of CLOS and less in the direction of
Java).

Michele Simionato

Nov 13 '06 #16

P: n/a
Doug wrote:
[...]
The explicit self is there simply because OOP was tacked onto python as
an afterthought.
No, it was added in such a way that Python would be useful as a
procedural as well as an OO language.

I don't know what's got into me.

can't-normally-say-boo-to-a-goose-ly y'rs - steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden

Nov 13 '06 #17

P: n/a
"Dennis Lee Bieber" <wl*****@ix.netcom.comwrote:

Especially since there is a comefrom <G>

*breaks into song* : "Oh Susannah, now don't you cry for me..."

Nov 13 '06 #18

P: n/a
You idiot. Putting the word "official" in front of something doesn't
mean it can't be FUD. Especially when it is written by people such as
yourself. Have you not paid attention to anything happening in
politics around the world during your lifetime?
Ridiculous boo-llshit!

Nov 13 '06 #19

P: n/a
On 11/13/06, Fredrik Lundh <fr*****@pythonware.comwrote:
>
I suppose that in his view, language advocacy is a zero-sum game, so
positive comments about Python can be considered as FUD against his own
project. He's even invented his own del.icio.us tag for this purpose:

http://del.icio.us/tag/pythonfud

now at:

http://del.icio.us/tag/python-fud

</F>
Hey, Fredrik, you have your own tag!
<http://del.icio.us/tag/fredrik-lundh-trollI wish *I* had my own
tag. ;-)

I notice that, as you predicted, Python's lack of a goto is in there too.

--
Cheers,
Simon B
si***@brunningonline.net
http://www.brunningonline.net/simon/blog/
Nov 13 '06 #20

P: n/a
Simon Brunning a écrit :
On 11/13/06, Fredrik Lundh <fr*****@pythonware.comwrote:
>>
I suppose that in his view, language advocacy is a zero-sum game, so
positive comments about Python can be considered as FUD against his own
project. He's even invented his own del.icio.us tag for this purpose:

http://del.icio.us/tag/pythonfud

now at:

http://del.icio.us/tag/python-fud

</F>

Hey, Fredrik, you have your own tag!
<http://del.icio.us/tag/fredrik-lundh-trollI wish *I* had my own
tag. ;-)

I notice that, as you predicted, Python's lack of a goto is in there too.
Hey, this is fun :D I also found that one :
http://del.icio.us/dholton/python-fud-by-fredrik-lundh

But is seems they all point to the same article than yours so you get
the points for it ;)
Nov 13 '06 #21

P: n/a
Michele Simionato wrote:
Peter Maas wrote:
>All these reasons are valid and retained by the following suggestion: let
self be represented by the dot <snip>

This suggestion has been discussed in the past (I remember having the
same idea myself when I first learned Python). But at the end I believe
the explicit 'self' is a better solution, because there are cases where
it is useful to have it
Thanks, Michele. My intention wasn't to abandon explicit self but to replace
it by a shorter and unique entity. I searched a little for similar suggestions
in the past but didn't find them so I decided to post. I admit that my
suggestion was half-baked.

But at least I learned something: a heated debate isn't bound to become an
endless thread if the OP abstains from answering idiot replies ;)

--
Regards/Gruesse,

Peter Maas, Aachen
E-mail 'cGV0ZXIubWFhc0B1dGlsb2cuZGU=\n'.decode('base64')
Nov 13 '06 #22

P: n/a
Peter Maas wrote:
But at least I learned something: a heated debate isn't bound to become an
endless thread if the OP abstains from answering idiot replies ;)
trolling is trolling even if you use smilies. I'm sure you can find a
way to actually *contribute* to Python if you really want to...

</F>

Nov 13 '06 #23

P: n/a
Steven D'Aprano schrieb:
Implicit self will never be used for Python, because it is redundant so
long as there is a need for explicit self, and there is a need for
explicit self because there are a number of basic, dare I say
*fundamental* programming techniques that require an explicit self.

For example, delegation.

class MyClass(Parent):
def method(self, arg):
Parent.method(self, arg)
# what are you going to write? Parent.method(,arg) maybe?
The idea is: let self (or self.) be represented by the dot alone,
therefore:

Parent.method(., arg) # :)
Here's another fundamental technique that implicit self breaks.

class MyList(list):
def __iadd__(self, other):
# in-place addition
other = transform(other) # do something to the argument
super(MyList, self).__iadd__(other) # call the superclass
# could be written .__class__.__iadd__(other)
# BUT be aware the semantics are NOT the same!
return self # and return the suitably modified instance
return .
You can't explicitly return the instance unless you have an explicit name
for it. (And if you are thinking of creating more magic syntax that
implicitly returns self, just don't bother.)
No magic. Just a dot. But perhaps a dot is too tiny. We could take JUST_ME
or ME_AND_BOBBY_MCGEE instead, of course as a reserved keyword followed by a
dot ;)

Thanks for your hints, Steven.

--
Regards/Gruesse,

Peter Maas, Aachen
E-mail 'cGV0ZXIubWFhc0B1dGlsb2cuZGU=\n'.decode('base64')
Nov 13 '06 #24

P: n/a
Peter Maas wrote:
No magic. Just a dot. But perhaps a dot is too tiny. We could take JUST_ME
or ME_AND_BOBBY_MCGEE instead, of course as a reserved keyword followed
by a
dot ;)
Why a dot, and not a @, like in Ruby and Perl?

I think a dot is a particular bad idea, not the least due to poorer
readability of code (and thou shall not underestimate the readability of
code!).

Baalbek
Nov 18 '06 #25

This discussion thread is closed

Replies have been disabled for this discussion.