Module properties for C modules
Hello Python Dev! As you all know modules don't support properties. However several places and modules could use properties on module level. For example the sys.py3k_warnings flag could be implemented with a property. Other flags in the sys module could benefit from read only properties, too. How do you like the general idea of properties for builtin modules. That is modules written in C like the sys module. Christian
On Wed, Apr 30, 2008 at 4:17 PM, Christian Heimes <lists@cheimes.de> wrote:
Hello Python Dev!
As you all know modules don't support properties. However several places and modules could use properties on module level. For example the sys.py3k_warnings flag could be implemented with a property. Other flags in the sys module could benefit from read only properties, too.
Big +1. Frankly, the get/set methods of sys are quite ugly!
How do you like the general idea of properties for builtin modules. That is modules written in C like the sys module.
Good idea. Perhaps eventually they could be extended to Python, but they are definitely useful in C now. How about passing a list of getsets to PyImport_InitModule(5)?
Christian _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/musiccomposition%40gmail.c...
-- Cheers, Benjamin Peterson
Benjamin Peterson schrieb:
Good idea. Perhaps eventually they could be extended to Python, but they are definitely useful in C now. How about passing a list of getsets to PyImport_InitModule(5)?
Yeah, I've a similar idea with PyImport_InitModule5() and a list of structs containing name, getter, setter, deleter, docstring. The module struct has to gain an additional slot which may contain a dict of names -> propertyobjects. Christian
On Wed, Apr 30, 2008 at 2:17 PM, Christian Heimes <lists@cheimes.de> wrote:
As you all know modules don't support properties. However several places and modules could use properties on module level. For example the sys.py3k_warnings flag could be implemented with a property. Other flags in the sys module could benefit from read only properties, too.
How do you like the general idea of properties for builtin modules. That is modules written in C like the sys module.
But wouldn't this mean that those properties would no longer be available in the module's __dict__? -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum schrieb:
But wouldn't this mean that those properties would no longer be available in the module's __dict__?
Correct. Module properties would behave exactly like instance properties. They don't appear on the instance's __dict__ attribute, too. By the way I was astonished that the vars() function dones't show properties but dir() does list them.
class Example(object): ... @property ... def x(self): ... return 42 ... example = Example() example.__dict__ {} vars(example) {} dir(example) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'x']
Christian
On Thu, May 1, 2008 at 12:32 PM, Christian Heimes <lists@cheimes.de> wrote:
Guido van Rossum schrieb:
But wouldn't this mean that those properties would no longer be available in the module's __dict__?
Correct. Module properties would behave exactly like instance properties. They don't appear on the instance's __dict__ attribute, too.
By the way I was astonished that the vars() function dones't show properties but dir() does list them.
"Astonished" sounds stronger than you probably meant it. :-)
class Example(object): ... @property ... def x(self): ... return 42 ... example = Example() example.__dict__ {} vars(example) {} dir(example) ['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__', 'x']
They are intentionally different though -- dir() tries to give all the attributes, while vars() only accesses __dict__. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum schrieb:
On Thu, May 1, 2008 at 12:32 PM, Christian Heimes <lists@cheimes.de> wrote:
Guido van Rossum schrieb:
But wouldn't this mean that those properties would no longer be available in the module's __dict__?
Correct. Module properties would behave exactly like instance properties. They don't appear on the instance's __dict__ attribute, too.
By the way I was astonished that the vars() function dones't show properties but dir() does list them.
"Astonished" sounds stronger than you probably meant it. :-)
Slightly :) What's your opinion on the module properties idea? Do you still like it although the property values won't show up in __dict__? Christian
On Thu, May 1, 2008 at 1:20 PM, Christian Heimes <lists@cheimes.de> wrote:
What's your opinion on the module properties idea? Do you still like it although the property values won't show up in __dict__?
I don't see how it could work if any Python code is executed in the module, since code execution uses a dict for globals. Supporting it only for built-in modules seems too big an exception. So I'm -0. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
Guido van Rossum schrieb:
I don't see how it could work if any Python code is executed in the module, since code execution uses a dict for globals.
Supporting it only for built-in modules seems too big an exception.
I came up with the idea in order to replace the setters and getters of builtin modules such as the sys module. I deliberately excluded Python modules from the idea. It's going to be too tricky to expose the API to Python. Christian
On Thu, May 1, 2008 at 2:00 PM, Christian Heimes <lists@cheimes.de> wrote:
Guido van Rossum schrieb:
I don't see how it could work if any Python code is executed in the module, since code execution uses a dict for globals.
Supporting it only for built-in modules seems too big an exception.
I came up with the idea in order to replace the setters and getters of builtin modules such as the sys module. I deliberately excluded Python modules from the idea. It's going to be too tricky to expose the API to Python.
Then I propose to drop the idea. -- --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Benjamin Peterson
-
Christian Heimes
-
Guido van Rossum