Read-only attribute in module

Terry Reedy tjreedy at
Thu Feb 9 14:15:45 EST 2012

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

More information about the Python-list mailing list