Simple prototyping in Python

Dave Benjamin ramen at lackingtalent.com
Sun May 2 22:31:53 EDT 2004


In article <c74830$hrfbu$1 at ID-169208.news.uni-berlin.de>, Greg Ewing wrote:
> Dave Benjamin wrote:
>> Hey, I never knew that "this" could be used inside of an anonymous object
>> in JavaScript. Thanks for pointing that out!
>> 
>> In Python, you'd have to give the object a name, since there's no "self" to
>> refer to.
> 
> No, you wouldn't, because the Python equivalent would
> be something like
> 
> def F():
>    c = [105]
> 
>    class C:
>      a = 5
> 
>      def incr(self):
>        self.a += 1
>        c[0] += 1
>        return [self.a, c[0]]
> 
>    return C()

I don't know if I'd call it equivalent. Similar in effect, naybe, but the
idea was to create an anonymous, classless object. 

> It's a bit more awkward in Python due to the inability to
> directly rebind a name in an outer scope.

This is an issue, but not a serious one. In my original example, I used a
pretty simple workaround (an anonymous object).

> BTW, IMO this is a seriously warped technique that I
> would never use, even in Javascript. I can't see any
> benefit in it that's anywhere near worth the obfuscation.

One benefit, which applies equally to both languages, is that this technique
gets you closer to enforced private attributes than the standard ways of
making objects. I'm sure you could hack frames or something, but "c" is
pretty much inaccessible to the casual programmer in your example above.

-- 
.:[ dave benjamin: ramen/[sp00] -:- spoomusic.com -:- ramenfest.com ]:.
:  please talk to your son or daughter about parametric polymorphism. :



More information about the Python-list mailing list