Macros in Python?

Dominic oblivious at web.de
Fri Apr 11 02:12:13 EDT 2003


Johann Hibschman wrote:
> Dominic <oblivious at web.de> writes:
> 
> 
>>>Alternatively, you can curry the above function:
>>>    def with_root_privs(func):
>>>        def call(func, *args, **kwargs):
>>>            save_euid = os.geteuid()
>>>            os.seteuid(0)
>>>            try:
>>>                func(*args, **kwargs)
>>>            finally:
>>>                os.seteuid(save_euid)
>>>        return lambda *args, **kwargs: call(func, *args, **kwargs)
> 
> 
>>Thanks. This one is also very good. :-)
>>Interesting idea to use a closure to build
>>a function wrapper.
> 
> 
> Hm.  Why not just do:
> 
> def with_root_privs(func):
>     def call(*args, **kwargs):
>         save_euid = os.geteuid()
>         os.seteuid(0)
>         try:
>             func(*args, **kwargs)
>         finally:
>             os.seteuid(save_euid)
>     return call
> 
> I don't see what that extra layer of indirection is gaining you.
Well, you could use the lambda construct to create
parametrized methods. In this example it does not matter
though.
Like a Lisp closure the lambda expression binds the
variables within the function call. Unlike Lisp
they cannot be changed lateron as far as I know.


def make_adder(value):
    def add(a,value):
       return a+value
    return lambda a: value+x

add3=make_adder(3)
print add(3)

I haven't tried it, but should work.

Ciao,
  Dominic


> 
> -Johann





More information about the Python-list mailing list