On 2/9/2012 6:43 AM, Mateusz Loskot wrote:
Hi,

I'm implementing Python 3 extension using the Python C API. I am
familiar with defining new types, implementing get/set for
attributes, etc.

I'm wondering, is there any mean to implement attribute in module
scope which is read-only?

Not that I know of. Python intentionally leaves modules mutable even after the code has executed so they can be monkey-patched from outside. The argument for is that it is unusual to do so but sometimes very useful and necessary. The main argument against is that it prevents optimizations that would make code in modules run faster.

import xyz print(xyz.flag)  # OK
> xyz.flag = 0 # error due to no write access

Why prevent that? If you called it 'FLAG', that would indicate that it is a constant that should not be changed. While Python make some effort to prevent bugs, it is generally a 'consenting adults' language.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to