Explicit Frustration of the Self
Sean Ross
frobozz_electric at hotmail.com
Sat Jan 4 12:17:15 EST 2003
"Terry Reedy" <tjreedy at udel.edu> wrote in message
news:WhqdnbfA_sxi9I-jXTWcqw at comcast.com...
>But of course, the first argument *is* passed as an argument -- which
>you can do explicitly if you do not like the usual abbreviated form.
>>>> class c:
>... def test(s): print 231
>...
>>>> ci = c()
>>>> c.test(ci)
>231
Of course.
However my point was, or rather should have been, that this usage is
uncommon or, surely, less common than
>>>ci=c()
>>>ci.test()
>231
Anyway...
When I first began learning python(and I still am learning), I found the
practice of passing self as an argument to its
instance methods hackish. I didn't mind the explicit use of self(and I still
don't - in fact I prefer it) but I didn't understand why passing it to its
own methods was necessary. It looked like it had been tacked on. To me, it
looked like someone trying to do OO in C.
In fact, I still didn't see any reason for it until -
>Bjorn Peterson wrote:
>class Foo:
> bar = 123
> def baz(,arg):
> class inner:
> def baz(,arg):
> .arg = arg # <- Foo's arg or inner's arg?
> return inner()
To date, I have never encountered this particular wrinkle in my python
programming, i.e., I've not yet had cause to implement an inner class. But,
once I saw this example, it helped clear my understanding of why self might
need to be passed to its methods.
The only workaround I could come up with was to add a keyword 'outer'. Then:
class Foo:
def self.baz(arg1):
self.data = arg1
class inner:
def self.baz(arg2):
self.data = arg2
outer.self.data = arg2
return inner()
I'm not sure whether I prefer this to -
class Foo:
def baz(outer_self, arg1):
outer_self.data = arg1
class inner:
def baz(inner_self, arg2):
inner_self.data = arg2
outer_self.data = arg2
return inner()
The first one _looks_ more object oriented... to me... But, more keywords is
not necessarily a good thing and the second example, I think, has greater
clarity.
Anyway, I'll have to think some more about which I prefer...
I wonder, how do they deal with this issue in Ruby?
Oh, just one more thing...
[snip]
> And, of course, calling the first arg 'self' is a convention, not a
> commandment.
>
> Terry J. Reedy
>
Of course...
> "Sean Ross" <sross at connectmail.carleton.ca> wrote in message
> news:uzlQ9.2011$8n5.515861 at news20.bellglobal.com...
>The use of self is so ubiquitous in python:
>You can say,
>
> def method(this, *args):
> pass
>
>but few if any do for the simple reason that just about everyone else uses
self.
>
More information about the Python-list
mailing list