Too many 'self' in python.That's a big flaw in this language.

H

hide1713

HI
I'm currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That's a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.
From My point,I think this only help python interpreter to deside
where to look for.
Is there anyone know's how to make the interpreter find instance name
space first?
Or any way to make programmer's life easier?
 
M

Marc 'BlackJack' Rintsch

In <[email protected]>,
I'm currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That's a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.

where to look for.
Is there anyone know's how to make the interpreter find instance name
space first?
Or any way to make programmer's life easier?

Use a shorter name than `self` or an editor with auto completion. If a
name in a function or method is local or global is decided at compile
time, not at run time. So at least every assignment to an instance
attribute must have the ``self.`` in front or the compiler sees the name
as local to the method. Changing this would slow down the interpreter
because every name has to be looked up in the instance dict every time to
decide if it's an attribute or a local name.

Another drawback of your proposed "magic" is that attributes can be
assigned, deleted or delegated dynamically at run time. So your bare `aa`
name can change meaning from instance attribute to local name or vice
versa over the time.

You must have very compelling reasons to ask for changes that spare you
some keystrokes by the way. Pythonistas usually don't like sacrificing
readability for fewer characters. Most source code will be written once
but must be read and understood a couple of times, so it's more important
to have clear than short code. With `self` in place you always know which
names are local and which are attributes.

Ciao,
Marc 'BlackJack' Rintsch
 
N

Neil Cerutti

HI
I'm currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That's a big inconvenience in coding ,especially when you have
lot of variable If you method need 10 variables ,you have to
type "self" for 10 times and that also makes your variable
longer.

I recommend the discussion of this issue in the Python FAQ.

http://www.python.org/doc/faq/gener...ed-explicitly-in-method-definitions-and-calls
From My point,I think this only help python interpreter to
deside where to look for. Is there anyone know's how to make
the interpreter find instance name space first? Or any way to
make programmer's life easier?

Try thinking of "self." as a notation that provides vital
information to you, the programmer.
 
F

faulkner

HI
I'm currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That's a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.


where to look for.
Is there anyone know's how to make the interpreter find instance name
space first?
Or any way to make programmer's life easier?

http://www.voidspace.org.uk/python/weblog/arch_d7_2006_12_16.shtml#e584
 
R

Roy Smith

faulkner said:

I looked the "Selfless Python" idea described there, and I think it's a
REALLY bad idea. It's a clever hack, but not something I would ever want
to see used in production code. Sure, it saves a little typing, but it
invokes a lot of magic and the result is essentially a new language and
everybody who uses this code in the future will have to scratch their heads
and figure out what you did.

Programs get written once. They get read and maintained forever, by
generations of programmers who haven't even been hired by your company yet.
Doing some clever magic to save a few keystrokes for the original
programmer at the cost of sowing confusion for everybody else in the future
is a bad tradeoff.
 
R

Roy Smith

Marc 'BlackJack' Rintsch said:
Use a shorter name than `self` or an editor with auto completion.

Of the two, I'd strongly vote for the auto completion (assuming you feel
the need to "solve" this problem at all). The name "self" is so ingrained
in most Python programmers minds, that it's almost a keyword. Changing it
to "this" or "s" or "me" will just make your program a little harder for
other people to understand.

Changing it to "this" would be particularly perverse since it's not even
any less typing. In fact, on a standard keyboard, it's harder to type
since it involves moving off the home row more :)
 
S

Sion Arrowsmith

Neil Cerutti said:
Try thinking of "self." as a notation that provides vital
information to you, the programmer.

And it provides even more vital information to *other* programmers
dealing with your code ("other" including "you in six months time").
I've just confused the socks off a cow-orker by writing in a C++
method kill(SIGTERM); -- confusion which would have been avoided if
I'd used an explicit this->kill(SIGTERM); . But amongst C++'s many
flaws, such disambiguation is frowned on as non-idiomatic. Explicit
self *is a good thing*.
 
J

Jorgen Bodde

I had the same feeling when I started, coming from a C++ background, I
forgot about self a lot, creating local copies of what should be an
assign to a class instance, or methods that could not be found because
I forgot 'self' .

Now I am 'kinda' used to it, as every language has some draw backs
(you can't please all). But, what about something in between like only
using the dot (.) for a shorter notation?

self.some_var = True

Could become:

..some_var = True

Which basically shows about the same thing, but you leave 'self' out
of the syntax. Ofcourse it should not be allowed to break a line
between the dot and the keywords, else Python would never know what to
do;

my_class()
..my_var = True

Should not be parsed the same as;

my_class().my_var = True

Just a suggestion. I am pretty happy with self, but I could settle for
a shorter version if possible.

- Jorgen
 
A

A.T.Hofkamp

HI
I'm currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

c++ is a much more static language (for example, you cannot create new fields
in your class at run time), so it can decide in advance what you mean.

In other words, it is a cost you pay for the increased flexibility. You may not
be using that flexibility, but it is there, and people use it.
That's a big inconvenience in coding ,especially when you have lot of
variable

I have switched from c++ to Python several years ago, and was surprised about
having to explicitly write 'self' each time. However, I never considered it "a
big inconvenience".
As years went by I have come to like the explicit notation in Python.

Or any way to make programmer's life easier?

Others have already pointed out that leaving out 'self' is more bad than good.
I think they are right. In the past I also thought that Python was badly
designed, and until now, in the end it appeared that I was always in error.
[off-topic:
I think that again now with the default implementation of the object.__eq__ and
object.__hash__ methods. I believe these methods should not exist until the
programmer explicitly defines them with a suitable notion of equivalence.

Anybody have a good argument against that? :)
]


Another suggestion may be to look at your code again, and check whether all
self's are really needed. In other words, can you improve your code by reducing
use of instance variables?
In Python, The "a=b" statement is extremely cheap, because you don't copy data.
Exploit that feature.

An alternative may be to copy a self variable into a local variable one and use
the local variable in the method. Another option may be to collect results in a
local variable first and then assign it to a self.X variable.

If you really have a lot of variables, are you sure that they should all be
seperate (flat) variables in one class, ie would it be possible to merge some
of them together in another object and have more structure in the variables?
(classes are much cheaper in Python than in c++ w.r.t. amount of code)


Sincerely,
Albert
 
B

Bjoern Schliessmann

I'm currently using Python.

How long have you been using Python?
I find that a instance variable
must confined with self, for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa #
See .if in c++,I could use aa to change that variable

Mh, strange, I personally like to use "this.a" in C++, to make clear
I use an instance variable.
That's a big inconvenience in coding ,especially when you have lot
of variable

NACK, see above.
If you method need 10 variables ,you have to type "self" for 10
times and that also makes your variable longer.

Explicit is better than implicit.
From My point,I think this only help python interpreter to deside
where to look for.

IMHO, it's also a great hint for the programmer. With others' C++
code, I'm often confused what kinds of variables (global, instance,
static, ...) they access, it's also badly commented. If C++ forced
the programmer to write "this.var", the code would be
understandable with much less comments.

Regards,


Björn
 
A

Alex Martelli

A.T.Hofkamp said:
I think that again now with the default implementation of the
object.__eq__ and object.__hash__ methods. I believe these methods should
not exist until the programmer explicitly defines them with a suitable
notion of equivalence.

Anybody have a good argument against that? :)

It's very common and practical (though not ideologically pure!) to want
each instance of a class to "stand for itself", be equal only to itself:
this lets me place instances in a set, etc, without fuss.

I don't want, in order to get that often-useful behavior, to have to
code a lot of boilerplate such as
def __hash__(self): return hash(id(self))
and the like -- so, I like the fact that object does it for me. I'd
have no objection if there were two "variants" of object (object itself
and politically_correct_object), inheriting from each other either way
'round, one of which kept the current practical approach while the other
made __hash__ and comparisons abstract.

In Python 3000, ordering comparisons will not exist by default (sigh, a
modest loss of practicality on the altar of purity -- ah well, saw it
coming, ever since complex numbers lost ordering comparisons), but
equality and hashing should remain just like now (yay!).


Alex
 
J

John Roth

HI
I'm currently using Python. I find that a instance variable must
confined with self,
for example:
class a:
def __init__(self):
self.aa=10
def bb(self):
print self.aa # See .if in c++,I could use aa to change that
variable

That's a big inconvenience in coding ,especially when you have lot of
variable
If you method need 10 variables ,you have to type "self" for 10 times
and that also makes your variable longer.


where to look for.
Is there anyone know's how to make the interpreter find instance name
space first?
Or any way to make programmer's life easier?


Guido has already said that this will not change in Python 3.0 See PEP
3099.

John Roth
 
A

Aahz

In Python 3000, ordering comparisons will not exist by default (sigh, a
modest loss of practicality on the altar of purity -- ah well, saw it
coming, ever since complex numbers lost ordering comparisons), but
equality and hashing should remain just like now (yay!).

While emotionally I agree with you, in practice I have come to agree
with the POV that allowing default ordering comparisons between disjoint
types causes subtle bugs that are more difficult to fix than the small
amount of boilerplate needed to force comparisons when desired.
 
A

Andy Freeman

How about "Mavis Beacon Teaches Typing"?

How about no "wouldn't it be better" suggestions until at least three
months after the suggester has written at least 1000 lines of working
code.?
 
E

Erik Max Francis

Aahz said:
While emotionally I agree with you, in practice I have come to agree
with the POV that allowing default ordering comparisons between disjoint
types causes subtle bugs that are more difficult to fix than the small
amount of boilerplate needed to force comparisons when desired.

I agree. It makes more sense to have to specify an ordering rather than
assume an arbitrary one that may or may not have any relation to what
you're interested in.

I always did think that the inability to compare complex numbers was a
bit of a wart -- not because it's not mathematically correct, since it
is, but rather since everything else was comparable, even between
distinct types -- but I think the more sensible approach is to allow
equality by default (which defaults to identity), and only support
comparisons when the user defines what it is he wants them to mean.
 
B

Bruno Desthuilliers

Jorgen Bodde a écrit :
I had the same feeling when I started, coming from a C++ background, I
forgot about self a lot, creating local copies of what should be an
assign to a class instance, or methods that could not be found because
I forgot 'self' .

Now I am 'kinda' used to it, as every language has some draw backs
(you can't please all). But, what about something in between like only
using the dot (.) for a shorter notation?

self.some_var = True

Could become:

.some_var = True

Which basically shows about the same thing, but you leave 'self' out
of the syntax. Ofcourse it should not be allowed to break a line
between the dot and the keywords, else Python would never know what to
do;

my_class()
.my_var = True

Should not be parsed the same as;

my_class().my_var = True

Just a suggestion. I am pretty happy with self, but I could settle for
a shorter version if possible.
What is nice with the required, explicit reference to the instance -
which BTW and so far is not required to be *named* 'self' - is that it
avoids the need for distinct rules (and different implementations) for
functions and methods. The different 'method' types are just very thin
wrappers around function objects. Which in turn allow to use 'ordinary'
functions (defined outside a class) as methods - IOW, to dynamically
extend classes (and instances) with plain functions. Uniformity can also
have very practical virtues...
 
A

Alex Martelli

Bjoern Schliessmann <[email protected]>
wrote:
...
Mh, strange, I personally like to use "this.a" in C++, to make clear
I use an instance variable.

That would be nice, unfortunately your C++ compiler will refuse that,
and force you to use this->a instead;-).

Many programming shops use naming conventions instead, such as my_a or
a_ (trailing underscore for member-variables) -- I've even seen the
convention this_a which IMHO is silly (at that point you might as well
use this->a and avoid the 'convention'!-).

Anyway, I essentially agree with you (except for the C++ bit: since this
is a pointer, it needs ->). However, full disclosure, Smalltalk/XP
superstar Kent Beck disagrees -- in his good book "Test Driven Design by
Example", in the chapter where he gives the Python example, he DOES
whine against the need to explicitly say self (the one bad bit in the
book:).

For the curious: the explicit-self idea is essentially taken from
Modula-3, a sadly now forgotten language which still had an impact on
the history of programming.


Alex
 
B

Bjoern Schliessmann

Alex said:
Bjoern Schliessmann <[email protected]>
wrote:
That would be nice, unfortunately your C++ compiler will refuse
that, and force you to use this->a instead;-).

Sure, thanks. Before I last used C++ I was forced to use Java --
where I would write "this. said:
Many programming shops use naming conventions instead, such as
my_a or a_ (trailing underscore for member-variables) -- I've even
seen the convention this_a which IMHO is silly (at that point you
might as well use this->a and avoid the 'convention'!-).
ACK.

For the curious: the explicit-self idea is essentially taken from
Modula-3, a sadly now forgotten language which still had an impact
on the history of programming.

Mh, I'm going to read some about this one.

Regards,


Björn
 
L

Lou Pecora

"Jorgen Bodde said:
I had the same feeling when I started, coming from a C++ background, I
forgot about self a lot, creating local copies of what should be an
assign to a class instance, or methods that could not be found because
I forgot 'self' .

Now I am 'kinda' used to it, as every language has some draw backs
(you can't please all). But, what about something in between like only
using the dot (.) for a shorter notation?

self.some_var = True

Could become:

.some_var = True

Which basically shows about the same thing, but you leave 'self' out
of the syntax. Ofcourse it should not be allowed to break a line
between the dot and the keywords, else Python would never know what to
do;

my_class()
.my_var = True

Should not be parsed the same as;

my_class().my_var = True

Just a suggestion. I am pretty happy with self, but I could settle for
a shorter version if possible.

- Jorgen

Hmmm... I like this idea. Would you put a dot in the argument of a
class method?

def afcn(.,x,y):
# stuff here

??

I still like it. self remains a wart on python for me after 5 years of
use despite a deep love of the language and developers' community.
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top