473,657 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Proposal for removing self

When doing object-oriented stuff, it bothers me to have to type "self" so
many times. I propose that Python allow the programmer to optionally type
".variable" instead of "self.varia ble" to mean the same thing. Of course,
the interpreter would have to be more careful about detecting floats that
begin with just a period as in ".5". What are your thoughts?
Jul 18 '05 #1
15 1950
Brent W. Hughes wrote:
When doing object-oriented stuff, it bothers me to have to type
"self" so many times. I propose that Python allow the programmer to
optionally type ".variable" instead of "self.varia ble" to mean the
same thing. Of course, the interpreter would have to be more careful
about detecting floats that begin with just a period as in ".5".
What are your thoughts?


When I started doing Python, it annoyed me. Now many years later
I definitely wouldn't change it. Guido has very good taste!

Roger
Jul 18 '05 #2
"Brent W. Hughes" <br**********@c omcast.net> wrote in message news:<T_7Xc.613 60$Fg5.2281@att bi_s53>...
When doing object-oriented stuff, it bothers me to have to type "self" so
many times. I propose that Python allow the programmer to optionally type
".variable" instead of "self.varia ble" to mean the same thing. Of course,
the interpreter would have to be more careful about detecting floats that
begin with just a period as in ".5". What are your thoughts?


There's a FAQ on the subject:

http://www.python.org/doc/faq/genera...ions-and-calls
Raymond Hettinger
Jul 18 '05 #3
"Brent W. Hughes" <br**********@c omcast.net> wrote in message news:<T_7Xc.613 60$Fg5.2281@att bi_s53>...
When doing object-oriented stuff, it bothers me to have to type "self" so
many times. I propose that Python allow the programmer to optionally type
".variable" instead of "self.varia ble" to mean the same thing. Of course,
the interpreter would have to be more careful about detecting floats that
begin with just a period as in ".5". What are your thoughts?


There's a FAQ on the subject:

http://www.python.org/doc/faq/genera...ions-and-calls
Raymond Hettinger
Jul 18 '05 #4
"Brent W. Hughes" <br**********@c omcast.net> wrote in message news:<T_7Xc.613 60$Fg5.2281@att bi_s53>...
When doing object-oriented stuff, it bothers me to have to type "self" so
many times. I propose that Python allow the programmer to optionally type
".variable" instead of "self.varia ble" to mean the same thing. Of course,
the interpreter would have to be more careful about detecting floats that
begin with just a period as in ".5". What are your thoughts?


I know that this has been proposed before and am familiar with the
alternatives commonly suggested. I understand that most Python
programmers are happy with things the way they are in this respect.
But after 3 years of Python, somehow, I still keep forgetting to type
the darn thing. Of course, I always catch the omission quickly but
that makes me wish there was a better alternative. Python has
generally been about less typing and self seems somewhat contrary to
that.

This following is not a carefully thought one but I would be
interested to know what your opinions are. The following is inspired
by win32all.

<from Python COM docs>
class HelloWorld:
_public_methods _ = ['Hello']
_public_attrs_ = ['softspace', 'noCalls']
_readonly_attrs _ = ['noCalls']

def __init__(self):
pass
</from Python COM docs>

How about having an optional attribute for a class that negates the
requirement for self?

One advantage with this is I can see all the member variables in one
place. Great while reading code.

<sample>
class FooBar:
__public__ = [Name, Age]
def setName(name):
Name = name
</sample>

While I am complaining, a few other peeves ...

Another thing is I dislike is having to use __ for *every* private
variable. For *me* __private__ at the top of the class definition code
would be more elegant. __readonly__ would be another nicety.

Introducing the above attributes into the language would not likely
break any existing Python code.

Just my 2c.
Jul 18 '05 #5

There was the "with" keyword :

with self:
.member = something
.member2 = .member3 + .member4

etc... it is a bit more implicit because of the "with"... anyway.
Jul 18 '05 #6
Brent W. Hughes wrote:
When doing object-oriented stuff, it bothers me to have to type "self" so
many times. I propose that Python allow the programmer to optionally type
".variable" instead of "self.varia ble" to mean the same thing. Of course,
the interpreter would have to be more careful about detecting floats that
begin with just a period as in ".5". What are your thoughts?

You don't have to use self. You could simply use underscore '_' instead.

class Person:

def setName(_, name):
_.name = name

def getName(_):
return _.name
But other Python programmers will hate you for it ;-)
If it is because you are using an attribute too many times inside a
method, a better solution is to map it to a local variable. It also has
the advantage of being faster.
class SoftwareProject Estimator:

pi = 3.14

def guestimatePrice (self, hours, hour_price, developer_group _size):
pi = self.pi
guess = hours * pi
guess *= pi * developer_group _size
return guess * hour_price
regards Max M
Jul 18 '05 #7
Andrea Griffini <ag****@tin.i t> wrote:
On 25 Aug 2004 22:23:46 -0700, ra****@cps.cmic h.edu (Ishwar Rattan)
wrote:
I concur for keeping self intact, it does provide clarity in code writing
and reading.
I remember Alex Martelli advocating in our italian newsgroup on
C++ that C++ programmers should use "this->..." everywhere :-)


I did that, and I did that before becoming mad with love for Python.

At the time my main job was helping hundreds of other programmers debug
their hairiest bugs (also helping them architect and design and test
things correctly, but debugging took far more time -- there's never time
to do it right so there's always time to do it over) and the lack of
this-> was a major cause of problems. confusions and bugs.
Seemed crazy to me at that time, but now after giving a try
to python I've to say that self-consciousness isn't bad.


If you write and debug truly complicated C++ templates you'll see why I
wanted explicit this-> prefixing most ardently...;-).
Alex
Jul 18 '05 #8
Ravi Teja Bhupatiraju <we*********@ya hoo.com> wrote:
...
<sample>
class FooBar:
__public__ = [Name, Age]
def setName(name):
Name = name
</sample>
Go ahead: learn about metaclasses and bytecode hacking and write your
custom metaclass to perform this. Except for the little detail that
you'll never get away with not quoting 'Name' and 'Age' in the
__public__ assignment, the rest is reasonably easy to perform. Publish
your metaclass and start campaigning for it.

As long as you're just yakking about it ain't gonna happen -- become a
metaclass and bytecode expert and you can implement this in Python, or
convince somebody who is such an expert, for which the implementation
should be the job of a couple days tops.

While I am complaining, a few other peeves ...

Another thing is I dislike is having to use __ for *every* private
variable. For *me* __private__ at the top of the class definition code
would be more elegant. __readonly__ would be another nicety.
That's an even easier job for a custom metaclass with very modest amount
of byecode hacking. I suggest you start with this one, really truly
VERY easy. In fact you could probably do it without any actual hacking
on the bytecode itself, just rewriting a few tables of strings in the
code objects of the functions that are the methods of the class.

One impossible task (THAT would require hacking the Python parser and
changing things very deeply) as I already mentioned would be the form
you want for the assignment to __public__ (or similarly to
__private__etc) . Those names are undefined (or worse defined globally)
and to teach Python (and human readers!) that for that one speclal case
of assignment to __public__ (or __private__), THAT one case only,
COMPLETELY different rules apply than ANYWHERE else in Python, well,
it's just as difficult as it is absurd.

I suggest you tackle the 'look ma no quotes!' task last, after
everything else is working, because that one detail is going to take far
longer than everything else put together, even if you're learning about
metaclasses and bytecodes from scratch.

Introducing the above attributes into the language would not likely
break any existing Python code.
If this peculiar behavior is limited to classes whose metaclass is your
custom RaviSpecial, "not likely" becomes "absolutely impossible", a
great enhancement. One more reason to implement this in a metaclass.
Just my 2c.


Well make it three cents and do an implementation, if you really care.
Otherwise, it's just words...
Alex
Jul 18 '05 #9
"Brent W. Hughes" <br**********@c omcast.net> writes:
When doing object-oriented stuff, it bothers me to have to type "self" so
many times.
When doing object-oriented stuff, it makes me extactic to write
"self." in front of instance attributes. The knowledge that other
programmers are always doing the same, makes me even more happy.

The fact that there are plenty of C++ and Java coding conventions
which require that all class members' names start with "m_", or that
all instance attributes be accessed, in methods, via "this->" or
"this." (depending) on the language in question, seems to support
Python's choice in this matter.
I propose that Python allow the programmer to optionally type
".variable" instead of "self.varia ble" to mean the same thing. Of course,
the interpreter would have to be more careful about detecting floats that
begin with just a period as in ".5". What are your thoughts?


I think you should get into the habit of reading FAQs, Archives,
Googling (you know, the usual stuff), before making such suggestions
publically.
Jul 18 '05 #10

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

Similar topics

9
2547
by: Jess Austin | last post by:
hi, I like the way that Python does lists, and I love the way it does iterators. But I've decided I don't like what it does with iterators of lists. Lists are supposed to be mutable sequences, but try to use an iterator of a list that you're mutating and watch it all fall to pieces. That is, if you change the length of a section of the list through which the iterator has already passed, it will lose track of where it is. I think...
0
1069
by: Ravi Teja Bhupatiraju | last post by:
>> Well make it three cents and do an implementation, if you really care. >> Otherwise, it's just words... I can't post a reply to that thread in Google. So I am posting the third cent here. Here is my implementation of the PythonCOM style __private__ () attributes for Python. http://aspn.activestate.com/ASPN/docs/ActivePython/2.3/PyWin32/html/com/win32com/HTML/QuickStartServerCom.html
31
2419
by: Brian Sabbey | last post by:
Here is a pre-PEP for what I call "suite-based keyword arguments". The mechanism described here is intended to act as a complement to thunks. Please let me know what you think. Suite-Based Keyword Arguments ----------------------------- Passing complicated arguments to functions is currently awkward in Python. For example, the typical way to define a class property winds up polluting the class's namespace with the property's get/set...
15
2586
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:
18
2265
by: Ralf W. Grosse-Kunstleve | last post by:
My initial proposal (http://cci.lbl.gov/~rwgk/python/adopt_init_args_2005_07_02.html) didn't exactly get a warm welcome... And Now for Something Completely Different: class autoinit(object): def __init__(self, *args, **keyword_args): self.__dict__.update(
47
3347
by: Pierre Barbier de Reuille | last post by:
Please, note that I am entirely open for every points on this proposal (which I do not dare yet to call PEP). Abstract ======== This proposal suggests to add symbols into Python. Symbols are objects whose representation within the code is more important than their actual value. Two symbols needs only to be
17
2436
by: Steve R. Hastings | last post by:
I have been studying Python recently, and I read a comment on one web page that said something like "the people using Python for heavy math really wish they could define their own operators". The specific example was to define an "outer product" operator for matrices. (There was even a PEP, number 211, about this.) I gave it some thought, and Googled for previous discussions about this, and came up with this suggestion: User-defined...
9
2026
by: corey.coughlin | last post by:
Alright, so I've been following some of the arguments about enhancing parallelism in python, and I've kind of been struck by how hard things still are. It seems like what we really need is a more pythonic approach. One thing I've been seeing suggested a lot lately is that running jobs in separate processes, to make it easy to use the latest multiprocessor machines. Makes a lot of sense to me, those processors are going to be more and...
3
11582
m6s
by: m6s | last post by:
Hello to all, I am having trouble removing an item from list, which the item is a list by itself. <code> for self.line in self.filtered: if self.appid: if self.line <> self.appid: print RED + " ".join( map ( lambda x:str(x), self.filtered )) + RESET self.filtered.remove( self.line.index(self.line ) ) else: print "F: " + str(self.line)
0
8411
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
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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
8613
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...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.