On Thu, Aug 06, 2020 at 11:22:38AM +1000, Chris Angelico wrote:
> On Thu, Aug 6, 2020 at 11:11 AM Steven D'Aprano <st...@pearwood.info> 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


*blinks*

When did this happen?

I'm on Python-Ideas, Python-Dev, and I get announcements of new issues 
on the bug tracker, and I don't recall ever seeing this feature 
discussed.

[looks up the docs]

Okay, apparently it was added in 3.6. But the documentation says:


"""
When using the default metaclass type, or any metaclass that ultimately 
calls type.__new__, the following additional customisation steps are 
invoked after creating the class object:

first, type.__new__ collects all of the descriptors in the class 
namespace that define a __set_name__() method;
"""

https://docs.python.org/3/reference/datamodel.html#class-object-creation

but that's not what is happening here, since my_property is not a 
descriptor, it's just an arbitrary instance.

(To be a descriptor, it needs to have `__get__` and/or `__set__` 
methods.)

Have I missed something or does this need a documentation fix?



-- 
Steven
_______________________________________________
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/SC6LDISDE2JRFLLBMZNSNHH2SO6ILTAM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to