[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question on difference between LambdaType and FunctionType

Chris Angelico <rosuav at gmail.com> wrote:
> [...] why do you care about the difference? What is it in your code that
> cares about whether a function was created with "def" or with "lambda"?

In answer to my previous question on the list,
where I asked about possible implementation of immutability for user-defined
types, dieter suggested a solution using metaclasses and instrumentation
of the __init__method:

In this solution, every __init__ method (be it in the class itself or somewhere
in super), sets the flag before it starts executing its body and
unsets it after it's
finished. The flag then is used in __setattr__ to determine whether to raise

Therefore, I'm writing a piece of code to directly modify __init__'s AST by
inserting two ast.Assign's into the tree (via the ast.NodeTransformer).
(Wrapping __init__ with decorator doesn't do the job.)

Everything works fine, except that I have to cover the following:

    foo = lambda self: None

    class Foo:
        __init__ = foo

Since I'm treating FunctionType and LambdaType differently (I don't have
to instrument lambdas, I can just return them), I have to know which one
is it.

Note: I know that there is a strong argument against using all this hackery,
however, I'm partly experimenting in an attempt to learn something new. :)

Iwo Herka