On Sat, May 29, 2021 at 12:46:11PM +0100, Matt del Valle wrote:

> My view is that variable decorators should be legal for:
> 
> 1) assignment statements (so that the decorator can modify or replace the
> object that is getting assigned)

Can you explain what benefit a variable decorator in this case would 
have over a function call?

    @func
    var = expression

is, I expect, the same as:

    var = func(expression)

or with the assignment target magic:

    var = func('var', expression)


> 2) bare type-hints, since these are arguably *a sort of* assignment
> operation that still carries useful info a decorator might want to capture
> (you're not assigning the name and its value to globals(), but rather the
> name and its type to globals()['__annotations__'])

Once we move out of bare type hints into calling a function, it's not a 
bare type hint any more.

    @func
    var: int

if it calls func, is not the same as

    var: int

which has no runtime effect.



A note on augmented assignment:

> 3) Augmented assignment gets expanded out to its true meaning before being
> passed to the decorator, so for example:

The true meaning of augmented assignment is not to expand the line of 
code out to assignment with the operator, but a method call:

    target += expression

    # not this
    target = target + expression

    # but this instead:
    target = type(target).__iadd__(target, expression)

Only if `__iadd__` doesn't exist does the interpreter fall back on the 
plus operator. This is why classes can implement augmented assignment as 
inplace operators:

    L = [1, 2, 3]
    L += [4]  # modifies L in place

    L = L + [4]  # creates a new list and assigns to L


This is a significant difference as soon as you have multiple references 
to the original list.



-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/S2HUAW5ASQVBKERKHQ6SQLKDRLQPNT4K/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to