On Thu, Aug 6, 2020 at 11:11 AM Steven D'Aprano <[email protected]> wrote:
> [Dominik Vilsmeier]:
> > > That should be possible by doing `fred = my_property(42)` and defining
> > > `__set_name__` on the `my_property` class.
>
> Just because you define your own dunder method (which you shouldn't do,
> since dunders are reserved for the interpreter's use) doesn't make
> something which is a syntax error stop being a syntax error.
>

This isn't "defining your own dunder". The syntax as described already
works inside a class:

class my_property:
    def __init__(self, n):
        self.n = n
    def __set_name__(self, cls, name):
        print("I'm a property %r on class %s" % (name, cls.__name__))

class X:
    fred = my_property(42)

I'm a property 'fred' on class X


But AIUI this is implemented by type.__new__, so there's no useful way
to extend this to globals and/or locals.

ChrisA
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/[email protected]/message/RHSUZ5VWV2IHMBPBD7ZKVVVBVH5M7375/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to