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

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Jun 27 07:41:02 EDT 2007


In <1182942154.185461.89620 at e9g2000prf.googlegroups.com>,
hide1713 at gmail.com wrote:

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

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



More information about the Python-list mailing list