the annoying, verbose self

Looney, James B james.b.looney at lmco.com
Wed Nov 21 19:05:20 EST 2007


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: python-list-bounces+james.b.looney=lmco.com at python.org 
> [mailto:python-list-bounces+james.b.looney=lmco.com at python.org
> ] On Behalf Of braver
> Sent: Wednesday, November 21, 2007 4:52 PM
> To: python-list at python.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
> 



More information about the Python-list mailing list