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

Tripp Lilley tlilley@perspex.com
Mon, 27 Nov 2000 20:58:57 +0000 (/etc/localtime)


On Mon, 27 Nov 2000 echuck@mindspring.com wrote:

> You have lost me. Why do you want to hack on "obj.foo()"?

I refer you to our earlier discussion on selfish-devel, but to summarize
for others' benefit (or anguish, as the case may be):

	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 )


Taking first the case of the "scalar" slot:

	I want to define two attributes, one a simple value, one a method.
	When the slot "scalar" is retrieved as a simple attribute (ie:
	o.scalar), it will magically pop out of __dict__['scalar'] per
	normal Python getattr rules.

	On the other hand, when it's retrieved just prior to a
	CALL_FUNCTION bytecode (or by the PyObject_CallMethod call), it
	will try magically returning __dict__['__meth_scalar__'], a
	wrapper which handles "method" semantics for the slot. If that
	fails, it will try calling __getmethod__, and if that throws an
	AttributeError, it will fall back to normal semantics.

All of this is predicated on the setting of __use_getmethod__ or somesuch
in globals.

Now, the case of the "method" slot:

	When called as a simple scalar (ie: o.method), this would be
	trapped by either __getattr__, or by the new __attr_method__ hook
	proposed in PEP 213. Either of those would simply execute the
	method in-place, returning the resolved value.

	When called as a method, the contents of
	__dict__['__meth_method__'] would be returned, which happen to be
	a normal, old-fashioned method object, which is called as normal.

So, basically, it allows me to do away with the incredibly ugly
slot-wrapper crap I'm using right now to implement attr/method opacity.
For certain general cases, it's quite efficient. For other general cases,
the inefficiency is masked by other inefficiencies that aren't avoidable.
At least as far as I know right now :)

--
   Joy-Loving * Tripp Lilley  *  http://stargate.eheart.sg505.net/~tlilley/
------------------------------------------------------------------------------
   "There were other lonely singers / in a world turned deaf and blind
    Who were crucified for what they tried to show.
    Their voices have been scattered by the swirling winds of time,
    'Cause the truth remains that no one wants to know."

   - Kris Kristofferson, "To Beat the Devil"