[Python-Dev] Object customization

Ken Manheimer klm@digicool.com
Sun, 16 Apr 2000 18:09:18 -0400 (EDT)


On Sat, 15 Apr 2000, Fredrik Lundh wrote:

> Greg Stein <gstein@lyra.org> wrote:
> > On Fri, 14 Apr 2000, Barry A. Warsaw wrote:
> > > >>>>> "FL" == Fredrik Lundh <effbot@telia.com> writes:
> > > 
> > >     FL> fwiw, I'd love to see a good syntax for this.  might even
> > >     FL> change my mind...
> > > 
> > > def foo(x):
> > >     self.x = x
> > > 
> > > ? <ducking>  :)
> > 
> > Hehe... actually, I'd take Skip's "_.x = 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 = x

'self.x' would collide profoundly with the convention of using 'self' for
the instance-argument in bound methods.  

Here, foo.x assumes that 'foo' is not rebound in the context of the def -
the class, module, function, or wherever it's defined.  That seems like an
unnecessarily too strong an assumption.  

Both of these things suggest to me that we don't want to use a magic
variable name, but rather some kind of builtin function to get the object
(lexically) containing the block.

It's tempting to name it something like 'this()', but that would be much
too easily confused in methods with 'self'.

Since we're looking for the lexically containing object, i'd call it 
something like current_object().

class Something:
    """Something's cooking, i can feel it."""

    def help(self, *args):
        """Spiritual and operational guidance for something or other.

        Instructions for using help:
        ..."""
        print self.__doc__
        print current_object().__doc__
	if args:
            self.do_mode_specific_help(args)

I think i'd be pretty happy with the addition of 
__builtins__.current_object, and the allowance of arbitrary metadata with
functions (and other funtion-like objects like methods).

Ken
klm@digicool.com