conceiling function calls..

Alex Martelli aleax at aleax.it
Fri Nov 14 08:05:16 EST 2003


Carlo v. Dango wrote:

> Thanks for your reply... Unfortunately, I cannot truly see your
> suggestions before my eyes..

Yeah, sigh, I was keeping them shrouded in a pitiful veil of shame.
What you ask is so horrible, unPythonic, and deleterious, that it IS,
in a way, a shame that Python lets you do it at all.  However, it does.

Here, as in most other places, it DOES "give you enough rope to shoot
yourself in the foot" if you get "clever" enough (note that "clever"
is NOT seen as a _positive_ quality in Python...).  It comes down to
that "trust the programmer" dictum.  Sometimes I wonder how factually
founded it is.  Oh well, maybe it's an ethical rather than pragmatical
idea: trusting the programmers ensures your place in heaven, or
something.  I sure hope so... thus, here it comes.


>>> It is possible to conceil access to methods using the "property()"
>>> function so method access looks like field access.. is the same possible
>>> for functions (not taking any args... )
>>
>> It's possible as long as the functions are accessed as attributes of
>> some object -- it may be horrible, but you CAN, if you wish, write a
>> __getattribute__ for the "some object" to force any access to the
>> attribute to become a CALL to that attribute.
> 
> but then I'm forced to use the self pointer.. I want to conceil it.

That's a good part of what makes your request so horrible: you want
to conceal what Python makes a point of revealing.


>> You could, of course, wrap the function into an object (of the
>> same name if you wish) that calls it whenever "an attribute of that
>> name" is accessed (SHUDDER).
> 
> hmm Im not sure what you are suggesting here. Are you still making use of
> the self reference? or how would you construct this...
> 
> please note that it's my purpose to hide the user from whats really going
> on..

Yes, sigh -- the most horrible, evil purpose one might imagine, just
about.  I truly hope you reconsider your intended course of action.  Still:

To recap: you have a global function f which when called w/o arguments
returns an object on which attributes may be accessed, a la
    print f().someattr

You want to HIDE (shudder) the fact that a function is being called,
ensuring, instead, that just coding:
    print f.someattr
will call f secretly, behind the scenes.

If one was truly intent on perpetrating this horror, then:

class DontLetKidsSeeThisPlease(object):
    def __init__(self, f): self.__f = f
    def __getattr__(self, name): return getattr(self.__f(), name)
f = DontLetKidsSeeThisPlease(f)

there -- that's all there is to it.  If you ALSO want to still be
able to call f() explicitly, add one more line to the class:
    def __call__(self): return self.__f()
and now an explicit f() will also behave as a normal call to f.
[One can of course also add attribute setting, and all other operations,
but I do hope I don't have to show them all explicitly too...!!!]


Alex





More information about the Python-list mailing list