<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body text="#000000" bgcolor="#FFFFFF">
<p><br>
</p>
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.<br>
<br>
<br>
<i>/arry</i><br>
<br>
<div class="moz-cite-prefix">On 09/08/2017 01:45 PM, Ethan Furman
wrote:<br>
</div>
<blockquote type="cite" cite="mid:59B30159.6000800@stoneleaf.us">On
09/08/2017 12:44 PM, Larry Hastings wrote:
<br>
<br>
<blockquote type="cite">I've updated PEP 549 with a new
title--"Instance Descriptors" is a better name than "Instance
Properties"--and to
<br>
clarify my rationale for the PEP. I've also updated the
prototype with code cleanups and a new type:
<br>
"collections.abc.InstanceDescriptor", a base class that allows
user classes to be instance descriptors.
<br>
</blockquote>
<br>
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:
<br>
<br>
Python 2.7.6 (default, Oct 26 2016, 20:32:47)
<br>
[GCC 4.8.4] on linux2
<br>
Type "help", "copyright", "credits" or "license" for more
information.
<br>
--> import module_class
<br>
--> module_class.hello
<br>
'hello'
<br>
--> module_class.hello = 'hola'
<br>
--> module_class.hello
<br>
'hola'
<br>
<br>
And the code:
<br>
<br>
class ModuleClass(object):
<br>
@property
<br>
def hello(self):
<br>
try:
<br>
return self._greeting
<br>
except AttributeError:
<br>
return 'hello'
<br>
@hello.setter
<br>
def hello(self, value):
<br>
self._greeting = value
<br>
<br>
import sys
<br>
sys.modules[__name__] = ModuleClass()
<br>
<br>
I will admit I don't see what reassigning the __class__ attribute
on a module did for us.
<br>
<br>
--
<br>
~Ethan~
<br>
_______________________________________________
<br>
Python-Dev mailing list
<br>
<a class="moz-txt-link-abbreviated" href="mailto:Python-Dev@python.org">Python-Dev@python.org</a>
<br>
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/listinfo/python-dev">https://mail.python.org/mailman/listinfo/python-dev</a>
<br>
Unsubscribe:
<a class="moz-txt-link-freetext" href="https://mail.python.org/mailman/options/python-dev/larry%40hastings.org">https://mail.python.org/mailman/options/python-dev/larry%40hastings.org</a>
<br>
</blockquote>
<br>
</body>
</html>