conceiling function calls..

Hung Jung Lu hungjunglu at yahoo.com
Fri Nov 14 15:38:02 EST 2003


Alex Martelli <aleax at aleax.it> wrote in message news:<gq4tb.20498$9_.728759 at news1.tin.it>...
> 
> 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)

I think the original poster was not very clear in his writing. But he
did give an analogy. Let me try to guess what he wants.

For a property implemented with getter and setter accessor methods,
you can do a lot of things when the user accesses the property. A few
examples are: (a) dynamically retrieve/store the value from/to a
database, (b) do some logging or access security control, (c) return
different values depending on environmental circumstances, etc.

The original poster seems to want the same level of control for access
to a global object inside a module. That's all. That is, he probably
would like to: (a) dynamically assemble the object on the flight,
perhaps from a database, perhaps some meta-programming, notice that a
simple Python namespace entry CANNOT do this trick, because it always
points to the same object. Sure, one way out in Python is to make a
wrapper, but that's exactly what the original poster's "f" is about,
(b) do some logging or security control everytime the object is
accessed, (c) return different objects depending on environmental
circumstances, etc.

regards,

Hung Jung




More information about the Python-list mailing list