<div dir="ltr">How are metaclasses unpythonic? This is one of the more defining features of python. There will be no way to properly implement this without moving the descriptor up to the metaclass. When thinking about metaclasses, it is often best to just think of them as classes and other types as instances.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Aug 20, 2015 at 11:20 AM, Sven R. Kunze <span dir="ltr"><<a href="mailto:srkunze@mail.de" target="_blank">srkunze@mail.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sounds more like Thomas is looking for an Pythonic solution without messing around with sys.modules or metaclasses.<br>
<br>
I personally like the proposals on StackOverflow and don't see why it couldn't be made to work out of the box:<br>
<br>
<br>
class MyClass:<br>
    @classproperty<br>
    def prop(cls):<br>
        return '42'<br>
<br>
# module property<br>
@property<br>
def prop(mod):<br>
    return '42'<span class="im HOEnZb"><br>
<br>
<br>
<br>
On 20.08.2015 09:47, Nathaniel Smith wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Thu, Aug 20, 2015 at 12:20 AM, Andrew Barnert via Python-ideas<br>
<<a href="mailto:python-ideas@python.org" target="_blank">python-ideas@python.org</a>> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Modules are a more serious problem, because there's no immediate way to specify the type for a module object. You can work around it by, e.g., declaring a module subclass, copying the module's dict to an instance of that subclass, then replacing the entry in sys.modules, but this is more than a little ugly.<br>
<br>
There have been proposals to allow modules to specify a type (e.g., something similar to the way __metaclass__ worked in 2.x), and to make it easier to hook the import machinery to create modules of a custom type, and probably other variations on this. You might want to search this list and -dev for previous ideas, to find one you think should be reconsidered.<br>
</blockquote>
This is already done :-). Python 3.5 allows a module to contain code like:<br>
<br>
class MetaModule(types.ModuleType):<br>
     @property<br>
     def some_attribute(self):<br>
         print("Accessing module.some_attribute")<br>
         return 1<br>
<br>
sys.modules[__name__].__class__ = MetaModule<br>
<br>
See also<br>
<br>
     <a href="https://pypi.python.org/pypi/metamodule" rel="noreferrer" target="_blank">https://pypi.python.org/pypi/metamodule</a><br>
<br>
for utility code and a polyfill back to ancient Pythons.<br>
<br>
-n<br>
<br>
</blockquote>
<br></span><div class="HOEnZb"><div class="h5">
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br></div>