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.

On Thu, Aug 20, 2015 at 11:20 AM, Sven R. Kunze <srkunze@mail.de> wrote:
Sounds more like Thomas is looking for an Pythonic solution without messing around with sys.modules or metaclasses.

I personally like the proposals on StackOverflow and don't see why it couldn't be made to work out of the box:


class MyClass:
    @classproperty
    def prop(cls):
        return '42'

# module property
@property
def prop(mod):
    return '42'



On 20.08.2015 09:47, Nathaniel Smith wrote:
On Thu, Aug 20, 2015 at 12:20 AM, Andrew Barnert via Python-ideas
<python-ideas@python.org> wrote:
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.

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.
This is already done :-). Python 3.5 allows a module to contain code like:

class MetaModule(types.ModuleType):
     @property
     def some_attribute(self):
         print("Accessing module.some_attribute")
         return 1

sys.modules[__name__].__class__ = MetaModule

See also

     https://pypi.python.org/pypi/metamodule

for utility code and a polyfill back to ancient Pythons.

-n


_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/