osdir.com

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

Question on difference between LambdaType and FunctionType


On Mon, Nov 26, 2018 at 7:56 AM Chris Angelico <rosuav at gmail.com> wrote:
>
> On Mon, Nov 26, 2018 at 7:55 AM Iwo Herka <iwoherka at gmail.com> wrote:
> >
> > > class Foo:
> > >    def setup(self): ...
> > >    __init__ = lambda self: self.setup()
> >
> > Sorry, didn't address this. This is fine too, since I'm assuming that
> > only methods named __init__ are allowed to mutate the object.
> > Because 'setup' is not '__init__' it's disqualified.
> >
>
> Ahh, okay. In that case, yeah, you can safely ignore lambda
> functions... until Python 3.8, when assignment expressions become a
> thing. (See PEP 572.) So if you want to do something hacky like using
> the __name__, go for it.
>

Actually... it kinda depends on the point of the optimization. If you
want a quick check, look at __name__. If it's okay to spend a bit more
time figuring out whether to optimize or not, look for a STORE_ATTR
opcode. If there aren't any, there can't be any "self.foo = bar". (You
can't be certain of the converse, as "other_object.foo = bar" will
also show up as STORE_ATTR.) This is a bit more work, but it's
fundamentally non-hacky, because you're actually looking for the thing
you care about.

ChrisA