Modules could behave like new-style objects
I would like to see modules behave more like new-style objects. One should be able to add properties, descriptors, __getattr__, and such. One common use case for me is implementing wrapper modules that populate dynamically (like the ctypes.windll object - except as a module). -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer Zindagi Games
On Thu, Apr 02, 2009, Zac Burns wrote:
I would like to see modules behave more like new-style objects. One should be able to add properties, descriptors, __getattr__, and such.
One common use case for me is implementing wrapper modules that populate dynamically (like the ctypes.windll object - except as a module).
Guido can speak up for himself, but in the past he's been pretty negative about this idea; you may want to hunt up previous threads. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/ "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan
Thanks Aahz, I did a search for previous threads but all the keywords and combinations of keywords I could think of showed an overwhelming amount of unrelated things. Does anyone know where I can find this information? -- Zachary Burns (407)590-4814 Aim - Zac256FL Production Engineer Zindagi Games On Thu, Apr 2, 2009 at 2:23 PM, Aahz <aahz@pythoncraft.com> wrote:
On Thu, Apr 02, 2009, Zac Burns wrote:
I would like to see modules behave more like new-style objects. One should be able to add properties, descriptors, __getattr__, and such.
One common use case for me is implementing wrapper modules that populate dynamically (like the ctypes.windll object - except as a module).
Guido can speak up for himself, but in the past he's been pretty negative about this idea; you may want to hunt up previous threads. -- Aahz (aahz@pythoncraft.com) <*> http://www.pythoncraft.com/
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." --Brian W. Kernighan _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
Zac Burns wrote:
Thanks Aahz,
I did a search for previous threads but all the keywords and combinations of keywords I could think of showed an overwhelming amount of unrelated things. Does anyone know where I can find this information?
Here's an example thread: http://mail.python.org/pipermail/python-dev/2003-January/032106.html It would also pose some difficult technical questions - currently "attr", "globals()['attr']" and "sys.modules[__name__].attr" all access the same value in different ways. If descriptors are allowed on normal modules, should the first two expressions be changed to invoke them? Or will the 3 expressions no longer be alternate ways of saying the same thing? Given the behaviour of code in the body of a class definition, it would most likely be the latter, but it would take quite a bit of thought to figure out the full ramifications of that. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia ---------------------------------------------------------------
Zac Burns wrote:
On Thu, Apr 2, 2009 at 2:23 PM, Aahz <aahz@pythoncraft.com> wrote:
Guido can speak up for himself, but in the past he's been pretty negative about this idea; you may want to hunt up previous threads.
I think the basic objection is that it would be inconsistent, because __getattr__ et al only have effect when defined in the object's class, not the object itself, and modules are not classes. What might be workable is a way to specify the class you want a module to have, something like __moduleclass__ = MyFancyModule But that doesn't quite work, because it needs to get evaluated before creating the module object, but you need to execute the module code first so you can import MyFancyModule from somewhere... it all gets rather messy. -- Greg
Zac Burns schrieb:
I would like to see modules behave more like new-style objects. One should be able to add properties, descriptors, __getattr__, and such.
One common use case for me is implementing wrapper modules that populate dynamically (like the ctypes.windll object - except as a module).
Modules are new style classes and they behave exactly like new style classes. Perhaps you have missed the point that modules are *instances* of the module type? As you probably know one can't overwrite magic methods like __getattr__ on the instance. :) Christian
Christian Heimes schrieb:
Zac Burns schrieb:
I would like to see modules behave more like new-style objects. One should be able to add properties, descriptors, __getattr__, and such.
One common use case for me is implementing wrapper modules that populate dynamically (like the ctypes.windll object - except as a module).
Modules are new style classes and they behave exactly like new style classes. Perhaps you have missed the point that modules are *instances* of the module type? As you probably know one can't overwrite magic methods like __getattr__ on the instance. :)
But one could probably implement the module type's __getattr__ method to delegate to the instance, somehow. Yes, I know that Guido doesn't like it. What one could also do is to replace the module object in sys.modules with a magic object that specializes the behaviour. The 'include' function in this module does it: http://svn.python.org/view/ctypes/trunk/ctypeslib/ctypeslib/dynamic_module.py?revision=HEAD&view=markup -- Thanks, Thomas
participants (6)
-
Aahz
-
Christian Heimes
-
Greg Ewing
-
Nick Coghlan
-
Thomas Heller
-
Zac Burns