Thank you, Good information, thank you. Was not aware of __set_name__.
DG > On 19 Oct 2023, at 09:28, dn via Python-ideas <python-ideas@python.org> wrote: > > On 19/10/2023 18.29, Dom Grigonis wrote: >> def __get__(self, instance, owner): >> def __set__(self, instance, value): >> Is there a reason why `__set__` does not have owner in it’s arguments while >> `__get__` does? > > Is this a Python Idea? > > You may have only given us a couple of lines, when the scope of the question > is much wider... > > Be careful because these mechanisms were updated relatively-recently - and > thus there are differences between Python versions! > > > From my notes (please see code-example which appears to answer your question): > > The __set_name__() method is a special method in Python that is used in the > context of descriptors. It was introduced in Python 3.6 as a part of the > Descriptor Protocol. > > The purpose of the __set_name__() method is to allow descriptors to > automatically determine and store the name of the attribute they are assigned > to within the class. This method is called once during the creation of the > descriptor instance, and it receives two arguments: the owner class and the > name of the attribute. > > By implementing the __set_name__() method in a descriptor, you can access and > store the name of the attribute to which the descriptor is assigned. This can > be useful when you want to associate the descriptor with the attribute name > or perform any additional setup based on the attribute name. > > Here's an example to illustrate the usage of __set_name__(): > > ```python > class Descriptor: > def __set_name__(self, owner, name): > self.name = name > > def __get__(self, instance, owner): > if instance is None: > return self > return instance.__dict__.get(self.name) > > def __set__(self, instance, value): > instance.__dict__[self.name] = value > > class MyClass: > attribute = Descriptor() > > obj = MyClass() > obj.attribute = 42 > print(obj.attribute) # Output: 42 > ``` > > In the above code, the Descriptor class defines the __set_name__() method. > When the attribute descriptor is assigned to the attribute attribute of the > MyClass class, the __set_name__() method is automatically called with the > owner class (MyClass) and the attribute name (attribute). Inside this method, > we store the attribute name in the descriptor instance. > > Later, when we set obj.attribute = 42, the descriptor's __set__() method is > called, and the value is stored in the instance's __dict__ attribute using > the previously stored attribute name. > > By using __set_name__(), descriptors can dynamically associate themselves > with the attribute names they are assigned to, providing more flexibility and > customization. > > > Web.Refs: > https://docs.python.org/3/howto/descriptor.html > https://docs.python.org/3/reference/datamodel.html#descriptors > > -- > Regards, > =dn > _______________________________________________ > 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/L7WZA7GEW3TPYUA4NF6POHDL2FSF5TIY/ > Code of Conduct: http://python.org/psf/codeofconduct/ _______________________________________________ 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/GXXHZ4KPC5OJDKGMMOWSE6SH44EAHJYK/ Code of Conduct: http://python.org/psf/codeofconduct/