
Nick Coghlan, 30.01.2013 10:54:
On Wed, Jan 30, 2013 at 11:06 AM, Larry Hastings wrote:
Properties are a wonderful facility. But they only work on conventional objects. Specifically, they *don't* work on module objects. It would be nice to extend module objects so properties worked there too.
As MAL notes, the issues with such an approach are:
- code executed at module scope - code in inner scopes that uses "global" - code that uses globals() - code that directly modifies a module's __dict__
There is too much code that expects to be able to modify a module's namespace directly without going through the attribute access machinery.
The Cython project has been wanting this feature for years. We even considered writing our own Module (sub-)type for this, but didn't get ourselves convinced that all of the involved hassle was really worth it. The main drive behind it is full control over setters to allow for safe and fast internal C level access to module globals (which usually don't change from the outside but may...). Currently, users help themselves by explicitly declaring globals as static internal names that are invisible to external Python code. Allowing regular objects in sys.modules would be one way to do it, but these things are a lot more involved at the C level than at the Python level due to the C level module setup procedure. I wouldn't mind letting such a feature appear at the C level first, even though the Python syntax would be pretty obvious anyway. It's not like people would commonly mess around with sys.__dict__. (Although, many C modules have a Python module wrapper these days, not sure if and how this should get passed through.) Stefan