[Compiler-sig] Re: [selfish-devel] Ugly hacks: modifying instance_getattr

Guido van Rossum guido@python.org
Mon, 27 Nov 2000 15:51:35 -0500


> 	I have two slots, "scalar" and "method", which are, respectively,
> 	a scalar value and a method (bound using the "new" hacks). I want
> 	to access those slots according to the conventions established in
> 	the Self language, namely without regard to whether they're
> 	implemented as simple attributes or as methods. Thus:
> 
> 		# retrieve
> 
> 		o.scalar
> 		o.scalar( )
> 
> 		o.method
> 		o.method( )
> 
> 
> 		# set
> 
> 		o.scalar = 42
> 		o.scalar( 42 )
> 
> 		o.method = 25
> 		o.method( 25 )

For "set", this is possible using the __setattr__ hook.

But for "retrieve" it is impossible, and I strongly recommend against
it.

In Python bound methods are first-class objects and can be passed
around just like function pointers.  For example:

	l = [0,1,2,3]
	a = l.append
	a(4)
	a(5)
	print l		# [0,1,2,3,4,5]

Your hack would break this, and I object against calling the resulting
language "Python".

Instead, you can use __getattr__ to redirect any reference to o.scalar
to a method call, so that you can use what you call scalar notation
for method implementation.  In my eyes, this is better than what you
want!

(Also note that the Python compiler-sig is really intended for
discussions of new ways of compiling Python, not for discussions of
the existing Python compiler.)

--Guido van Rossum (home page: http://www.python.org/~guido/)