Hey guys I am thinking of perhaps writing a PEP to introduce user-defined constants to Python. Something along the lines of Swift’s “let” syntax (e.g. “let pi = 3.14”).
Do you guys think it would be a good idea? Why or why not? Do you think there’s a better way to do it? I’d like to know what others think about this idea before making any formal submission (I’ve already posted this same question on python-list, but I just wanted to gauge opinion here too).
I'm -1.
I feel Python is complex language already. And I don't want make it more complicate. INADA Naoki songofacandy@gmail.com
On Tue, Nov 21, 2017 at 3:33 PM, Saeed Baig saeedbaig616@icloud.com wrote:
Hey guys I am thinking of perhaps writing a PEP to introduce user-defined constants to Python. Something along the lines of Swift’s “let” syntax (e.g. “let pi = 3.14”).
Do you guys think it would be a good idea? Why or why not? Do you think there’s a better way to do it? I’d like to know what others think about this idea before making any formal submission (I’ve already posted this same question on python-list, but I just wanted to gauge opinion here too).
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
21.11.17 08:33, Saeed Baig пише:
Hey guys I am thinking of perhaps writing a PEP to introduce user-defined constants to Python. Something along the lines of Swift’s “let” syntax (e.g. “let pi = 3.14”).
Do you guys think it would be a good idea? Why or why not? Do you think there’s a better way to do it?
To do what? What problem do you need to solve?
How is that different from "pi = 3.14"?
On Tue, Nov 21, 2017 at 1:33 AM, Saeed Baig saeedbaig616@icloud.com wrote:
Hey guys I am thinking of perhaps writing a PEP to introduce user-defined constants to Python. Something along the lines of Swift’s “let” syntax (e.g. “let pi = 3.14”).
Do you guys think it would be a good idea? Why or why not? Do you think there’s a better way to do it? I’d like to know what others think about this idea before making any formal submission (I’ve already posted this same question on python-list, but I just wanted to gauge opinion here too).
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
Javascript (ES6) has 'let' and 'const' for mutable and constant variables, respectively.
When programming in JS, I find myself aggressively aiming for as little 'let' and as much 'const' as reasonably possible, since reasoning about constants is much easier than about variables. In this context, 'const' is used as a marker, a runtime checker, and, with proper tooling (IDE or linter), a coding-time reminder to differentiate between these two fundamental behaviours.
I'm not +1 on introducing this idea to Python yet, but not -1 either - this deserves some discussion, if this has not been discussed already.
Cheers,
S.
On Tue, Nov 21, 2017 at 9:13 AM, Steven D'Aprano steve@pearwood.info wrote:
On Tue, Nov 21, 2017 at 02:38:45AM -0500, Joseph Jevnik wrote:
How is that different from "pi = 3.14"?
pi = 3.14 pi = 5 print(pi) # prints 5
let pi = 3.14 pi = 5 # raises an exception
-- Steve _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
This could also be useful if one eventually wanted to implement constant optimizations in the interpreter as it would be possible to detect constants when they are declared without any code analysis.
This would be "constant" as in Java constants right? Referential constants, so that if an object were mutable you would still be able to change its internal state.
-1. I don't see how this would improve any programs I've written or seen. Tools like mypy or linters might benefit from a feature to track constants and ensure they don't get changed, but I don't think it's needed as a language feature. Seriously, has anyone ever written "math.pi = 5" and been surprised that their code broke? Or even modified an application constant like BUFFER_LIMIT? Do you have any evidence that this would avoid bugs in real-world code?
Paul
PS Please fix your font - your posts are coming through with a huge font size for some reason, which makes them extremely difficult to read.
On 21 November 2017 at 06:33, Saeed Baig saeedbaig616@icloud.com wrote:
Hey guys I am thinking of perhaps writing a PEP to introduce user-defined constants to Python. Something along the lines of Swift’s “let” syntax (e.g. “let pi = 3.14”).
Do you guys think it would be a good idea? Why or why not? Do you think there’s a better way to do it? I’d like to know what others think about this idea before making any formal submission (I’ve already posted this same question on python-list, but I just wanted to gauge opinion here too).
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
21.11.17 11:47, Paul Moore пише:
-1. I don't see how this would improve any programs I've written or seen. Tools like mypy or linters might benefit from a feature to track constants and ensure they don't get changed, but I don't think it's needed as a language feature. Seriously, has anyone ever written "math.pi = 5" and been surprised that their code broke? Or even modified an application constant like BUFFER_LIMIT? Do you have any evidence that this would avoid bugs in real-world code?
Actually the possibility of modifying application constants like BUFFER_LIMIT can be useful.
On 21 November 2017 at 10:47, Paul Moore p.f.moore@gmail.com wrote:
-1. I don't see how this would improve any programs I've written or seen. Tools like mypy or linters might benefit from a feature to track constants and ensure they don't get changed
It is actually likely that something like this will appear in ``typing``:
from typing import Final, List
x: Final = 42 x = 1 # Fails type check
lst: Final[List[int]] = [] lst.append(5) # OK lst = [1, 2, 3] # Fails type check
-- Ivan
Note that no other feature in typing is about the reference - everything is about the objects themselves. Final makes less sense as a general type. We can't enforce A[T]().foo() not to reassign a Final T.
Elazar
בתאריך יום ג׳, 21 בנוב׳ 2017, 12:42, מאת Ivan Levkivskyi < levkivskyi@gmail.com>:
On 21 November 2017 at 10:47, Paul Moore p.f.moore@gmail.com wrote:
-1. I don't see how this would improve any programs I've written or seen. Tools like mypy or linters might benefit from a feature to track constants and ensure they don't get changed
It is actually likely that something like this will appear in ``typing``:
from typing import Final, List x: Final = 42 x = 1 # Fails type check lst: Final[List[int]] = [] lst.append(5) # OK lst = [1, 2, 3] # Fails type check
-- Ivan
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
That's one way to do it with no changes to the language, though syntaxically I find it lacking a bit of elegance (maybe a matter of getting accustomed with it?).
Also, I'm not sure "Final" really conveys what it means (at first glance, I thought it was about immutability, not constantness).
Maybe "Const" would be better in this context ? (Or maybe you've discussed this question already and come to the conclusion that "Final" is better for some reason?)
S.
On Tue, Nov 21, 2017 at 11:41 AM, Ivan Levkivskyi levkivskyi@gmail.com wrote:
On 21 November 2017 at 10:47, Paul Moore p.f.moore@gmail.com wrote:
-1. I don't see how this would improve any programs I've written or seen. Tools like mypy or linters might benefit from a feature to track constants and ensure they don't get changed
It is actually likely that something like this will appear in ``typing``:
from typing import Final, List x: Final = 42 x = 1 # Fails type check lst: Final[List[int]] = [] lst.append(5) # OK lst = [1, 2, 3] # Fails type check
-- Ivan
Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On 21 November 2017 at 12:12, Stéfane Fermigier sf@fermigier.com wrote:
That's one way to do it with no changes to the language, though syntaxically I find it lacking a bit of elegance (maybe a matter of getting accustomed with it?).
Also, I'm not sure "Final" really conveys what it means (at first glance, I thought it was about immutability, not constantness).
Maybe "Const" would be better in this context ? (Or maybe you've discussed this question already and come to the conclusion that "Final" is better for some reason?)
It is not set in stone, but it looks like most people like Final (although the initial proposal was Const, see https://github.com/python/mypy/issues/1214)
-- Ivan
It is not set in stone, but it looks like most people like Final (although the initial proposal was Const, see https://github.com/python/ mypy/issues/1214)
Ivan, you mean this thread "Can't index named tuple by defined constant" https://github.com/python/mypy/issues/3078?
With kind regards, -gdg
On 21 November 2017 at 14:22, Kirill Balunov kirillbalunov@gmail.com wrote:
It is not set in stone, but it looks like most people like Final (although
the initial proposal was Const, see https://github.com/python/mypy /issues/1214)
Ivan, you mean this thread "Can't index named tuple by defined constant" https://github.com/python/mypy/issues/3078?
Discussions about Final/Const happened in several threads on both typing and mypy trackers, I just don't remember them all.
-- Ivan
2017-11-21 7:33 GMT+01:00 Saeed Baig saeedbaig616@icloud.com:
Hey guys I am thinking of perhaps writing a PEP to introduce user-defined constants to Python. Something along the lines of Swift’s “let” syntax (e.g. “let pi = 3.14”).
If you want to work on a PEP, you will have to write a strong rationale for it :-)
Do you guys think it would be a good idea? Why or why not? Do you think there’s a better way to do it? I’d like to know what others think about this idea before making any formal submission (I’ve already posted this same question on python-list, but I just wanted to gauge opinion here too).
Python has different kinds of namespaces: module globals, class attributes, function local variables, etc.
The https://github.com/fijal/quill programming language looks like Python but makes module globals *mapping* "immutable": setattr(module, 'var', new_value). Only the mapping is immutable, a value can be mutable. I guess that the motivation here is to help the optimizer to emit more efficient code.
See also previous attempts:
"PEP 416 -- Add a frozendict builtin type" https://www.python.org/dev/peps/pep-0416/ => my motivation was to develop a sandbox for Python
"PEP 351 -- The freeze protocol" https://www.python.org/dev/peps/pep-0351/ => I guess that the main motivation was to previous programming mistakes, misuse of an API
The question is if you only want to have a technical solution to prevent modification of module globals, or if you would like to advertize that a variable is constant and use it somehow.
Victor
ISTM that using the already widely used convention of ALL_CAPS for constants is plenty for linters to warn about rebinding names.
Python believes "we're all adults here" after all. So even though it's worth *warning* users if BUFFER_LIMIT gets redefined, there can be reasons between consenting adults for doing so when you know what you are doing.
It feels similar to the fact that I *can* call:
instance._Klass__secret()
But the spelling suggests a strong recommendation to use a more public API. Exact same story with redefining an implied constant.
On Tue, Nov 21, 2017 at 7:47 AM, Victor Stinner victor.stinner@gmail.com wrote:
2017-11-21 7:33 GMT+01:00 Saeed Baig saeedbaig616@icloud.com:
Hey guys I am thinking of perhaps writing a PEP to introduce user-defined constants to Python. Something along the lines of Swift’s “let” syntax
(e.g.
“let pi = 3.14”).
If you want to work on a PEP, you will have to write a strong rationale for it :-)
Do you guys think it would be a good idea? Why or why not? Do you think there’s a better way to do it? I’d like to know what others think about
this
idea before making any formal submission (I’ve already posted this same question on python-list, but I just wanted to gauge opinion here too).
Python has different kinds of namespaces: module globals, class attributes, function local variables, etc.
The https://github.com/fijal/quill programming language looks like Python but makes module globals *mapping* "immutable": setattr(module, 'var', new_value). Only the mapping is immutable, a value can be mutable. I guess that the motivation here is to help the optimizer to emit more efficient code.
See also previous attempts:
"PEP 416 -- Add a frozendict builtin type" https://www.python.org/dev/peps/pep-0416/ => my motivation was to develop a sandbox for Python
"PEP 351 -- The freeze protocol" https://www.python.org/dev/peps/pep-0351/ => I guess that the main motivation was to previous programming mistakes, misuse of an API
The question is if you only want to have a technical solution to prevent modification of module globals, or if you would like to advertize that a variable is constant and use it somehow.
Victor _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/