[Python-ideas] Should Python have user-defined constants?

Stéfane Fermigier sf at fermigier.com
Tue Nov 21 05:40:14 EST 2017


On Tue, Nov 21, 2017 at 11:03 AM, Serhiy Storchaka <storchaka at gmail.com>
wrote:

> 21.11.17 11:34, Saeed Baig пише:
>
>> Serhiy asked, in relation to constants, “To do what? What problem do you
>> need to solve?”. No problem in particular, to be honest. I just thought
>> they’d be nice since they’d increase confidence that my
>> variables-intended-to-be-constants wouldn’t get reassigned, and just to
>> increase readability/explicitness to other programmers.
>>
>
> For increasing readability/explicitness you can use a name convention
> (UPPER_CASE), comments and documentation.
>

UPPER_CASE variable names are a reasonable convention for global constants.

I wouldn't like to read something like:

for i in range(0, 100):
   FOO = f(i)
   BAR = g(i, FOO)
   do_something_with(BAR)

but rather:

for i in range(0, 100):
   const foo = f(i)
   const bar = g(i, foo)
   do_something_with(bar)

or:

for i in range(0, 100):
   let foo = f(i)
   let bar = g(i, foo)
   do_something_with(bar)

If we move on with this idea (which is a long shot of course), choosing
between 'let' and 'const' is a matter of taste and being consistent with
other languages.

As for increasing confidence that your variables-intended-to-be-constants
> wouldn’t get reassigned, you can use read-only properties. This is a tricky
> to implement read-only properties for modules, but see PEP 562 which should
> help to do this.
>
> Please don't use GIGANTIC font in your messages.
>

+1.

  S.


>
> And please don't create a new thread when answer.
>
> And *please* don't include unrelated text in your messages.
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>



-- 
Stefane Fermigier - http://fermigier.com/ - http://twitter.com/sfermigier -
http://linkedin.com/in/sfermigier
Founder & CEO, Abilian - Enterprise Social Software -
http://www.abilian.com/
Chairman, Free&OSS Group / Systematic Cluster -
http://www.gt-logiciel-libre.org/
Co-Chairman, National Council for Free & Open Source Software (CNLL) -
http://cnll.fr/
Founder & Organiser, PyData Paris - http://pydata.fr/
---
“You never change things by fighting the existing reality. To change
something, build a new model that makes the existing model obsolete.” — R.
Buckminster Fuller
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171121/7ed21317/attachment.html>


More information about the Python-ideas mailing list