explicit self revisited

P

Peter Maas

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?
 
F

Fredrik Lundh

Peter said:
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>
 
D

Doug

Fredrik said:
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.
 
S

skip

Doug> The explicit self is there simply because OOP was tacked onto
Doug> python as an afterthought.

Got a reference to support that claim?

Skip
 
S

Steven D'Aprano

Doug> The explicit self is there simply because OOP was tacked onto
Doug> python 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.
 
S

Steven D'Aprano

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)
 
D

Doug

Fredrik said:
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.
 
D

Duncan Booth

Dennis Lee Bieber said:
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!"
 
A

Alan G Isaac

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
 
C

Carsten Haese

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,_uncertainty_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
 
F

Fredrik Lundh

Carsten said:
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>
 
M

Michele Simionato

Peter said:
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
 
S

Steve Holden

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
 

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

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,665
Latest member
salkete

Latest Threads

Top