473,546 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

the annoying, verbose self

Is there any trick to get rid of having to type the annoying,
character-eating "self." prefix everywhere in a class? Sometimes I
avoid OO just not to deal with its verbosity. In fact, I try to use
Ruby anywhere speed is not crucial especially for @ prefix is better-
looking than self.

But things grow -- is there any metaprogramming tricks or whatnot we
can throw on the self?

Cheers,
Alexy
Nov 21 '07 #1
84 7147
braver wrote:
Is there any trick to get rid of having to type the annoying,
character-eating "self." prefix everywhere in a class? Sometimes I
avoid OO just not to deal with its verbosity. In fact, I try to use
Ruby anywhere speed is not crucial especially for @ prefix is better-
looking than self.

But things grow -- is there any metaprogramming tricks or whatnot we
can throw on the self?
The most common answer you are going to get is that explicit is better
than implicit, which I tend to agree with. Some people use 's' instead
of 'self'. This shortens the number of characters you have to type, and
is only one character more than the Ruby '@' prefix.

Here is a metaprogramming technique that removes self, but I don't
recommend these sorts of things, especially if other people are going to
be looking at your code in the future. So use it at your own risk!

http://aspn.activestate.com/ASPN/Coo.../Recipe/362305

-Farshid
Nov 22 '07 #2
There are always tricks. If 5 characters is really too much to type,
how about 2 characters "s.". Though I would recommend against that
since
it violates standard Python convention.

def foo( self ):

becomes

def foo( s ):

Otherwise, if you happen to be using self.something a lot, just
assign it to a variable, and use that. But be careful as that
can become a lot more difficult to read/maintain than simply
leaving self along to begin with.

ss = self.something
ss.foo()

To me, using 'self' in Python is no different than using some other
variable pointing to a class instance in a static C++ class function.

-James
-----Original Message-----
From: py************* *************** *************@p ython.org
[mailto:py****** *************** *************** *****@python.or g
] On Behalf Of braver
Sent: Wednesday, November 21, 2007 4:52 PM
To: py*********@pyt hon.org
Subject: the annoying, verbose self

Is there any trick to get rid of having to type the annoying,
character-eating "self." prefix everywhere in a class? Sometimes I
avoid OO just not to deal with its verbosity. In fact, I try to use
Ruby anywhere speed is not crucial especially for @ prefix is better-
looking than self.

But things grow -- is there any metaprogramming tricks or whatnot we
can throw on the self?

Cheers,
Alexy
--
http://mail.python.org/mailman/listinfo/python-list
Nov 22 '07 #3
On Wed, 21 Nov 2007 15:51:56 -0800, braver wrote:
Is there any trick to get rid of having to type the annoying,
character-eating "self." prefix everywhere in a class?

Oh I know! It' uch a pain. Sinc writing a hug cla lat wk, I'v had a
trribl hortag o lowrca S E L and F charactr. It mak writing vry annoying.

Sometimes I avoid OO just not to deal with its verbosity.
There are other values than brevity. In fact, brevity is one of the less
important values.

In fact, I try to use
Ruby anywhere speed is not crucial especially for @ prefix is better-
looking than self.
Well, it takes all sorts I suppose. Who would those with good taste mock
if everybody liked the same things?

But things grow -- is there any metaprogramming tricks or whatnot we can
throw on the self?
Oh yeah, that's just what I like to see! Complicated, brittle, hard to
debug, difficult to understand metaprogramming tricks in preference to a
simple, easy-to-read naming convention.

--
Steven.
Nov 22 '07 #4
On 2007-11-22, Steven D'Aprano <st****@REMOVE. THIS.cybersourc e.com.auwrote:
On Wed, 21 Nov 2007 15:51:56 -0800, braver wrote:
>Is there any trick to get rid of having to type the annoying,
character-eating "self." prefix everywhere in a class?
You got this highly flexible language, very good for rapid programming, no more
clutter from block brackets and variable declarations, and now this 'self' pops
up.

So much overhead....

Can the computer not decide by itself what I want stored in the object? It can
read my code too!

Oh I know! It' uch a pain. Sinc writing a hug cla lat wk, I'v had a
trribl hortag o lowrca S E L and F charactr. It mak writing vry annoying.
Yes annoying isn't it?

Last week I was programming in C++ again, and now I have this terrible sur-plus
of lowercase T H I and S letters. I don't understand how they do it (I used to,
but not any more).

Maybe we should setup an exchange program so everybody can trade letters with
each other.

>Sometimes I avoid OO just not to deal with its verbosity.

There are other values than brevity. In fact, brevity is one of the less
important values.
NO! You got it all wrong here! It is not about brevity, it is about SPEED.

With less letters, there is less to read, so it can be read faster, so it must
be better!

Just like "if x:" is MUCH BETTER than "if x != 0:"
The thing that should be fixed imho, is that the brain of some of us tries to
deduce higher levels of abstractions from what is essentially a very long line
of simple instructions.

>But things grow -- is there any metaprogramming tricks or whatnot we can
throw on the self?

Oh yeah, that's just what I like to see! Complicated, brittle, hard to
debug, difficult to understand metaprogramming tricks in preference to a
simple, easy-to-read naming convention.
Maybe we should state first which variables we want to store in self, and then
have a meta-programming hook that automatically puts assignments to those
variables into the object.
And while we are at it, maybe we should also state the type of data we want to
put in it. That should help too.

Now that would be progress.
Ah well, I guess we will have to wait a while before that happens.....
Albert
Nov 22 '07 #5
Alexy:
Sometimes I
avoid OO just not to deal with its verbosity. In fact, I try to use
Ruby anywhere speed is not crucial especially for @ prefix is better-
looking than self.
Ruby speed will increase, don't worry, as more people will use it.

Bye,
bearophile
Nov 22 '07 #6
On 22 Nov., 00:51, braver <delivera...@gm ail.comwrote:
But things grow -- is there any metaprogramming tricks or whatnot we
can throw on the self?
http://docs.python.org/lib/compiler.html

Nov 22 '07 #7
On Nov 22, 4:34 pm, Kay Schluehr <kay.schlu...@g mx.netwrote:
On 22 Nov., 00:51, braver <delivera...@gm ail.comwrote:
But things grow -- is there any metaprogramming tricks or whatnot we
can throw on the self?

http://docs.python.org/lib/compiler.html
Indeed. Well, my current solution is to bow to the collective wisdom
and retain the self, but type it with just one symbol

..->

.... in TextMate, with the Python bundle! (If only TextMate Python
bundle woulldn't indent everything it pastes.... And that's how Ruby
helps Python -- at the code's very inception TextMate!)

Cheers,
Alexy
Nov 22 '07 #8
On Thu, Nov 22, 2007 at 10:13:46AM +0100, A.T.Hofkamp wrote regarding Re: the annoying, verbose self:

On 2007-11-22, Steven D'Aprano <st****@REMOVE. THIS.cybersourc e.com.auwrote:
On Wed, 21 Nov 2007 15:51:56 -0800, braver wrote:
Is there any trick to get rid of having to type the annoying,
character-eating "self." prefix everywhere in a class?
You're tilting against windmills. This has been gone over many times. There are very valid reasons for the explicit self, which I understood well enough when I read about them to stop complaining about self, but not well enough to be able to justify to someone else. Sorry for that. But since Alexy likes finding old threads that address his concerns, maybe he'll hunt them down, himself.
You got this highly flexible language, very good for rapid programming, no more
clutter from block brackets and variable declarations, and now this 'self' pops
up.

So much overhead....

Can the computer not decide by itself what I want stored in the object? It can
read my code too!
But not every variable declared inside a class needs to be assigned to self.. Make judicious use of non-self variables, if you don't want to see self everywhere.
Oh I know! It' uch a pain. Sinc writing a hug cla lat wk, I'v had a
trribl hortag o lowrca S E L and F charactr. It mak writing vry annoying.
Yes annoying isn't it?

Last week I was programming in C++ again, and now I have this terrible sur-plus
of lowercase T H I and S letters. I don't understand how they do it (I used to,
but not any more).

Maybe we should setup an exchange program so everybody can trade letters with
each other.

Sometimes I avoid OO just not to deal with its verbosity.
There are other values than brevity. In fact, brevity is one of the less
important values.
NO! You got it all wrong here! It is not about brevity, it is about SPEED.
if you're looking for speed of typing, just type a one character self equivalent, like:

class q(object):
def method(;, arg):
;.var = 3

Then when you're done coding: :%s/;/self/g
With less letters, there is less to read, so it can be read faster, so itmust
be better!
Ah. Speed of reading. In fact, speed of reading is connected to clarity, not brevity. If you have to keep looking back to some declaration section to remember if a given variable is tied to self, it's going to slow down your comprehension speed. the written chinese language is far more concise than japansese, but it's faster to skim japanese, because of the three alphabet system. Most of the meaning is contained in kanji, while the hiragana supplies disambiguation and grammatical specifiers, so you know you can skip past the hiragana and still retain the basic meaning. Having self used by convention means that once you get used to seeing it, you don't need to read it any more. It would really mess with python coders, though, if each person made up their own self-equivalent. You write "s", I write "self", Larry Wall starts coding python and uses "my". Then we'd all have to actually pay attention, and reading really would slow down. That's probably why self seems to be the most consistently held convention in python. Occasionally, someone will use cls, but that's for a different context, where you really do want people to slow down and realise that something different is going on.
Just like "if x:" is MUCH BETTER than "if x != 0:"
Only when "if x:" is what you mean. The semantics are different:

py>>x = ''
py>>if x: print "x"
py>>if x != 0: print "x != 0"
x != 0

The thing that should be fixed imho, is that the brain of some of us tries to
deduce higher levels of abstractions from what is essentially a very longline
of simple instructions.
Sorry, no entiendo. What needs to be fixed here?
But things grow -- is there any metaprogramming tricks or whatnot we can
throw on the self?
Oh yeah, that's just what I like to see! Complicated, brittle, hard to
debug, difficult to understand metaprogramming tricks in preference to a
simple, easy-to-read naming convention.
Maybe we should state first which variables we want to store in self, andthen
have a meta-programming hook that automatically puts assignments to those
variables into the object.
And while we are at it, maybe we should also state the type of data we want to
put in it. That should help too.

Now that would be progress.
I hope you are taking the piss.

Ah well, I guess we will have to wait a while before that happens.....
Albert
Oh God. You are taking the piss. IHBT.

<sigh>
Nov 22 '07 #9
be************@ lycos.com wrote:
Alexy:
>Sometimes I
avoid OO just not to deal with its verbosity. In fact, I try to use
Ruby anywhere speed is not crucial especially for @ prefix is better-
looking than self.

Ruby speed will increase, don't worry, as more people will use it.

Bye,
bearophile
I don't see this as a big deal, but
suppose that the syntax were
expanded so that, in a method, a dot
".", as a precursor to an identifier,
was treated as "self." is currently treated?

References to the instance itself would
continue to use "self". A lonely
dot would get lost on the page.

Initially, this form could be treated as
an alternative, usage would
determine its fate. Thus "self.name" or
".name" would identify the same
object.

Colin W.
Nov 22 '07 #10

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

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.