[Python-Dev] PEP 549 v2: now titled Instance Descriptors

Larry Hastings larry at hastings.org
Fri Sep 8 16:51:54 EDT 2017


Assigning the module's __class__ means you can otherwise initialize your 
module instance normally--all your class needs to do is declare your 
properties or magic methods.  If you overwrite the sys.modules entry for 
your module with a new instance of a custom subclass, all your module 
initialization needs to be done within--or propagated to--the new class 
or instance.


//arry/

On 09/08/2017 01:45 PM, Ethan Furman wrote:
> On 09/08/2017 12:44 PM, Larry Hastings wrote:
>
>> I've updated PEP 549 with a new title--"Instance Descriptors" is a 
>> better name than "Instance Properties"--and to
>> clarify my rationale for the PEP.  I've also updated the prototype 
>> with code cleanups and a new type:
>> "collections.abc.InstanceDescriptor", a base class that allows user 
>> classes to be instance descriptors.
>
> I like the new title, I'm +0 on the PEP itself, and I have one 
> correction for the PEP:  we've had the ability to simulate module 
> properties for ages:
>
>     Python 2.7.6 (default, Oct 26 2016, 20:32:47)
>     [GCC 4.8.4] on linux2
>     Type "help", "copyright", "credits" or "license" for more 
> information.
>     --> import module_class
>     --> module_class.hello
>     'hello'
>     --> module_class.hello = 'hola'
>     --> module_class.hello
>     'hola'
>
> And the code:
>
>     class ModuleClass(object):
>         @property
>         def hello(self):
>             try:
>                 return self._greeting
>             except AttributeError:
>                 return 'hello'
>         @hello.setter
>         def hello(self, value):
>             self._greeting = value
>
>     import sys
>     sys.modules[__name__] = ModuleClass()
>
> I will admit I don't see what reassigning the __class__ attribute on a 
> module did for us.
>
> -- 
> ~Ethan~
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: 
> https://mail.python.org/mailman/options/python-dev/larry%40hastings.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170908/9f27b002/attachment.html>


More information about the Python-Dev mailing list