On 4 October 2017 at 17:15, Ian Kelly <ian.g.ke...@gmail.com> wrote:
> On Wed, Oct 4, 2017 at 9:08 AM, Steve D'Aprano
> <steve+pyt...@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

I wonder - would the people who want "real constants" find the
following confusing:

>>> from demo import foo
>>> foo = 14
>>> foo
14

It's fundamental to the way the import function works, and how names
in Python behave, but I can see someone with a background in other
languages with "real" constants thinking "but foo is a constant, and
importing it stops it being a constant!"

Paul
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to