Simple prototyping in Python
Greg Ewing
greg at cosc.canterbury.ac.nz
Sun May 2 21:42:54 EDT 2004
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()
It's a bit more awkward in Python due to the inability to
directly rebind a name in an outer scope.
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.
--
Greg Ewing, Computer Science Dept,
University of Canterbury,
Christchurch, New Zealand
http://www.cosc.canterbury.ac.nz/~greg
More information about the Python-list
mailing list