Constants [was Re: newb question about @property]
Ian Kelly
ian.g.kelly at gmail.com
Wed Oct 4 12:15:26 EDT 2017
On Wed, Oct 4, 2017 at 9:08 AM, Steve D'Aprano
<steve+python at pearwood.info> wrote:
> But in large projects, especially those where you cannot trust every module in
> the project to obey the naming convention, I can see that this lack might
> contribute to the perception, if not the fact, of Python being a bit too
> unsafe for big projects. We have read-only attributes in classes, but not
> read-only names in modules. That makes me a little bit sad.
Which brings up the point that you can hack it in if you want it.
$ cat demo.py
import sys
class DemoModule:
@property
def foo(self):
return 42
sys.modules['demo'] = DemoModule()
$ python3 -c 'import demo; print(demo.foo); demo.foo = 14'
42
Traceback (most recent call last):
File "<string>", line 1, in <module>
AttributeError: can't set attribute
More information about the Python-list
mailing list