[Python-Dev] Object customization

Fredrik Lundh Fredrik Lundh" <effbot@telia.com
Sat, 15 Apr 2000 14:02:02 +0200


Greg Stein <gstein@lyra.org> wrote:
> On Fri, 14 Apr 2000, Barry A. Warsaw wrote:
> > >>>>> "FL" =3D=3D Fredrik Lundh <effbot@telia.com> writes:
> >=20
> >     FL> fwiw, I'd love to see a good syntax for this.  might even
> >     FL> change my mind...
> >=20
> > def foo(x):
> >     self.x =3D x
> >=20
> > ? <ducking>  :)
>=20
> Hehe... actually, I'd take Skip's "_.x =3D x" over the above =
suggestion. The
> above syntax creates too much of an expectation to look for "self". =
There
> would, of course, be problems that self.x doesn't work in a method =
while
> _.x could.

how about the obvious one: adding the name of the
function to the local namespace?

    def foo(x):
        foo.x =3D x

(in theory, this might of course break some code.  but
is that a real problem?)

after all, my concern is that the above appears to work,
but mostly by accident:

>>> def foo(x):
>>>    foo.x =3D x
>>> foo(10)
>>> foo.x
10
>>> # cool. now let's try this on a method
>>> class Foo:
>>>    def bar(self, x):
>>>       bar.x =3D x
>>> foo =3D Foo()
>>> foo.bar(10)
Traceback (most recent call first):
NameError: bar
>>> # huh?

maybe making it work in both cases would help?

...

but on third thought, maybe it's sufficient to just keep the
"static variable" aspect out of the docs.  I just browsed a
number of javascript manuals, and I couldn't find a trace of
this feature.

so how about this?

    -0.90000000000000002 on documenting this as "this
    can be used to store static data in a function"

    +1 on the feature itself.

</F>