473,799 Members | 3,224 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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?

Jun 27 '07 #1
26 2662
In <11************ *********@e9g20 00prf.googlegro ups.com>,
hi******@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
Jun 27 '07 #2
On 2007-06-27, hi******@gmail. com <hi******@gmail .comwrote:
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/genera...ions-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.

--
Neil Cerutti
Jun 27 '07 #3
On Jun 27, 7:02 am, "hide1...@gmail .com" <hide1...@gmail .comwrote:
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?
http://www.voidspace.org.uk/python/w..._16.shtml#e584

Jun 27 '07 #4
faulkner <fa*********@gm ail.comwrote:
http://www.voidspace.org.uk/python/w..._16.shtml#e584
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.
Jun 27 '07 #5
Marc 'BlackJack' Rintsch <bj****@gmx.net wrote:
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 :-)
Jun 27 '07 #6
Neil Cerutti <ho*****@yahoo. comwrote:
>On 2007-06-27, hi******@gmail. com <hi******@gmail .comwrote:
>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.
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*.

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
"Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jun 27 '07 #7
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_v ar = True

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

- Jorgen
On 6/27/07, Roy Smith <ro*@panix.comw rote:
Marc 'BlackJack' Rintsch <bj****@gmx.net wrote:
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 :-)
--
http://mail.python.org/mailman/listinfo/python-list
Jun 27 '07 #8
On 2007-06-27, hi******@gmail. com <hi******@gmail .comwrote:
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
Jun 27 '07 #9
hi******@gmail. com wrote:
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

--
BOFH excuse #13:

we're waiting for [the phone company] to fix that line

Jun 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

75
3907
by: David MacQuigg | last post by:
Seems like we need a simple way to extend Python syntax that doesn't break existing syntax or clash with any other syntax in Python, is easy to type, easy to read, and is clearly distinct from the "base" syntax. Seems like we could put the @ symbol to good use in these situations. Examples: print @(separator = None) x, y, z @x,y:x*x+y*y -- anonymous function
2
1722
by: Calvin | last post by:
Hi All, Could someone tell me just how secure Python is if compiled to an exe? Is it more or less secure than using some other language? Thanks
15
2600
by: Ralf W. Grosse-Kunstleve | last post by:
****************************************************************************** This posting is also available in HTML format: http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html ****************************************************************************** Hi fellow Python coders, I often find myself writing:: class grouping:
8
2709
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am always doing why cant this be automated or abstracted away? Are the instances where I won't pass self? I imagine there is some tradeoff involved otherwise it would have been
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10247
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.