[Python-ideas] PEP: Shorthand Symbol for "self"

Thomas Lee tom at vector-seven.com
Mon Aug 25 15:13:11 CEST 2008


I really don't think any of the proposed solutions are logically, 
functionally or aesthetically better than what we currently have with 
"def foo(self)".

I find all proposed variants of "$.foo" to be jarring. "def self.bar()" 
is somewhat less ugly, but misleading since the method is actually being 
bound to its outer class, not "self" (presumably, any instances of that 
class rather than the class itself):

 >>> class Foo(object):
...     def bar(self):
...             print "Hello, 0x%x" % id(self)
...
 >>> foo = Foo()
 >>> Foo.bar(foo)
Hello, 0xb7729f8c
 >>> foo.bar()
Hello, 0xb7729f8c
 >>>

Note that the declaration of "bar" binds the function to class "Foo", 
not to the instance "foo". Would these semantics change with the 
"self.bar()" syntax?

Either way it's a -1 from me until a more Pythonic proposal surfaces. 
I'm not sure there's a way to improve on the simplicity of the existing 
semantics though.

Cheers,
T

Russ Paielli wrote:
>
>
> On Mon, Aug 25, 2008 at 12:45 AM, Bruce Leban <bruce at leapyear.org 
> <mailto:bruce at leapyear.org>> wrote:
>
>     Jim Jewett wrote:
>
>     If I could write:
>
>       class foo:
>         def self.bar():
>           self.rebar(self.babar)
>
>     then the call to  object.bar() would match the declaration.
>
>     Back to Russ's proposal: it would be better accomodated IMHO by
>     allowing $ as a character in a variable name, just like _ is.
>     Then, conventionally, people could use $ as self:
>
>       def $.bar():
>         $.rebar($.babar)
>
>     and for whatever it's worth, I find $.bar easier to read then $bar
>     as the explicit dot reminds me it's doing an attribute get rather
>     than looking like a special variable name.
>
>
>   def $.bar():
>     $.rebar($.babar)
>
> That's two separate proposals, but I think I like both of them.
>
> Of course, Python already allows "S", which is very similar to "$", as 
> the first argument, so we're almost there on that aspect of it. Come 
> to think of it, I may start using "S" and see how it works out.
>
> So how about
>
>     def S.bar():
>         S.rebar(S.babar)
>
> --Russ
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
>   




More information about the Python-ideas mailing list