: python-ideas@python.org
Subject: [Python-ideas] Re: Introduce constants in Python (constant name
binding)
Hello,
On Tue, 25 May 2021 11:53:20 -
"Shreyan Avigyan" wrote:
> I posted my previous idea regarding this on the mailing list. This
> idea is a little different. Th
Hello,
On Tue, 25 May 2021 11:53:20 -
"Shreyan Avigyan" wrote:
> I posted my previous idea regarding this on the mailing list. This
> idea is a little different. This idea suggests introducing constant
> name bindings. This is similar to const pointer in C/C++. Once a name
> has been assigne
Christopher Barker writes:
> i really hate lists that don't have the the list as the default reply
> setting :-(
This is something that lists should *not* do, because your MUA can and
should do it for you (and not for me). If you want the feature, get a
decent MUA (unfortunately, that probably
oops, forgot to include the list.
i really hate lists that don't have the the list as the default reply
setting :-(
(Yes, I know that goes against the conventional wisdom)
On Wed, May 26, 2021 at 9:14 AM Shreyan Avigyan
> wrote:
>
>> Well. How can I go beyond why constant was invented in the fi
On 26/05/2021 08:25, Shreyan Avigyan wrote:
Reply to Chris:
There are two things I want to say about constants :-
1) Global-Local Constants - The ALL_CAPS convention variables should become
constant.
Good luck enforcing that on every Python programmer. Should it apply to
variable names lik
I have certainly overridden sys.stdout, it's USEFUL, Shreyan. (One use
case was to make programs pause after each screenful of output. Probably
I've also used it to suppress output.)
But I didn't know about sys.__stdout__, thanks Steven.
And yes, I'm at least as confused as to what the proposal
Well. How can I go beyond why constant was invented in the first place? As far
as I can understand by my limited knowledge that *sometimes* constants can be
useful, sometimes they are terrible. The main reason constant was invented was
to provide an additional support to programmers so that they
Shreyan Avigyan writes:
> I've already given one. Since Python is dynamically typed changing
> a critical variable can cause huge instability. Want a
> demonstration? Here we go,
>
> import sys
> sys.stdout = None
>
> Now what?
> Now how can we print anything?
With print(). Even in yo
On 2021-05-26 13:53, Shreyan Avigyan wrote:
I've already given one. Since Python is dynamically typed changing a critical
variable can cause huge instability. Want a demonstration? Here we go,
import sys
sys.stdout = None
Now what? Now how can we print anything? Isn't this a bug? There are lot
On Wed, 26 May 2021 at 14:33, Shreyan Avigyan wrote:
>
> Reply to Paul Moore:
>
> > But you just said it was runtime, so it definitely *isn't* similar to
> > the syntax error "Can't assign to literal here". You're making
> > inconsistent statements again :-(
>
> That's exactly why I wrote SomeErro
On Wed, May 26, 2021 at 11:20 PM Shreyan Avigyan
wrote:
>
> Reply to Chris:
>
> Yes I know that. sys.stdout exists there for that reason only. But if we
> can't print then it means we changed it somewhere. I just gave an example.
Yes, an example where you're trying to show... something. Which I
Reply to Paul Moore:
> But you just said it was runtime, so it definitely *isn't* similar to
> the syntax error "Can't assign to literal here". You're making
> inconsistent statements again :-(
That's exactly why I wrote SomeErrorType instead of SyntaxError. They are never
similar. I just said t
On Wed, May 26, 2021 at 12:53:32PM -, Shreyan Avigyan wrote:
> I've already given one. Since Python is dynamically typed changing a critical
> variable can cause huge instability. Want a demonstration? Here we go,
>
> import sys
> sys.stdout = None
>
> Now what? Now how can we print anything
Reply to Chris:
Yes I know that. sys.stdout exists there for that reason only. But if we can't
print then it means we changed it somewhere. I just gave an example. I've seen
code where constants can be really necessary. Python lets us use these things
because it's a programming language. But li
On Wed, May 26, 2021 at 11:15 PM Steven D'Aprano wrote:
>
> On Wed, May 26, 2021 at 10:37:34PM +1000, Chris Angelico wrote:
>
> > Also, what about this:
> >
> > constant x = 10
> > def f():
> > x = 20
> >
> > SyntaxError? Runtime error? Shadowing?
>
> The x inside the function is just a local
On Wed, May 26, 2021 at 10:37:34PM +1000, Chris Angelico wrote:
> Also, what about this:
>
> constant x = 10
> def f():
> x = 20
>
> SyntaxError? Runtime error? Shadowing?
The x inside the function is just a local variable. The x outside the
function and the x inside it are in different na
On 2021-05-26 at 12:53:32 -,
Shreyan Avigyan wrote:
> I've already given one. Since Python is dynamically typed changing a
> critical variable can cause huge instability. Want a demonstration?
> Here we go,
>
> import sys
> sys.stdout = None
>
> Now what? Now how can we print anything? Isn'
On Wed, 26 May 2021 at 13:13, Shreyan Avigyan wrote:
>
> Reply to Paul Moore:
>
> In Python terms, a constant is a name that binds itself to a value in memory
> and that name cannot bind itself to a different value now (unlike variables).
> The value can be mutated (if mutable) but the name cann
On Wed, May 26, 2021 at 12:31:07PM -, Shreyan Avigyan wrote:
> Reply to Paul Moore:
>
> if some_condition:
> constant a = 1
> else:
> a = 2
> a = 3
>
> Yes this is allowed. This is runtime.
Do you have any suggestions for how this should be implemented, how the
interpreter will know
On Wed, May 26, 2021 at 10:53 PM Shreyan Avigyan
wrote:
>
> I've already given one. Since Python is dynamically typed changing a critical
> variable can cause huge instability. Want a demonstration? Here we go,
>
> import sys
> sys.stdout = None
>
> Now what? Now how can we print anything? Isn't
I've already given one. Since Python is dynamically typed changing a critical
variable can cause huge instability. Want a demonstration? Here we go,
import sys
sys.stdout = None
Now what? Now how can we print anything? Isn't this a bug? There are lots of
code out there where we need to protect
On Wed, May 26, 2021 at 1:38 PM Shreyan Avigyan
wrote:
> Reply to Stestagg:
>
> That's annotation to make sure no one uses a name.
No.
> This is a proposal to implement constant name binding in Python.
Here are some excerpts from pep 591, specifically, how are they different
from what you a
On Wed, May 26, 2021 at 10:37 PM Shreyan Avigyan
wrote:
>
> Reply to Stestagg:
>
> That's annotation to make sure no one uses a name. This is a proposal to
> implement constant name binding in Python. There are many applications of
> constants.
>
Yes, and you've given us zero of them. Can you s
On Wed, May 26, 2021 at 10:31 PM Shreyan Avigyan
wrote:
>
> Reply to Paul Moore:
>
> if some_condition:
> constant a = 1
> else:
> a = 2
> a = 3
>
> Yes this is allowed. This is runtime.
>
> for i in range(10):
> constant a = []
>
> Not sure. Though it's preferable to be runtime. Prefe
Reply to Stestagg:
That's annotation to make sure no one uses a name. This is a proposal to
implement constant name binding in Python. There are many applications of
constants.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe sen
Reply to Paul Moore:
if some_condition:
constant a = 1
else:
a = 2
a = 3
Yes this is allowed. This is runtime.
for i in range(10):
constant a = []
Not sure. Though it's preferable to be runtime. Preferable is "not allowed".
And lists are also literals. Any Python Object that is not
On Wed, May 26, 2021 at 1:10 PM Shreyan Avigyan
wrote:
> Reply to Paul Moore:
>
> In Python terms, a constant is a name that binds itself to a value in
> memory and that name cannot bind itself to a different value now (unlike
> variables). The value can be mutated (if mutable) but the name canno
On Wed, 26 May 2021 at 12:59, Shreyan Avigyan wrote:
> 4. constant pi = 3.14
> # later
> pi = 3.1415 # Error
Steven's already asked what error, and is it compile time or runtime. I'll add
foo.py:
constant x = 12
bar.py
import foo
foo.x = 19
You can only detect this at runtime.
baz.p
Reply to Paul Moore:
In Python terms, a constant is a name that binds itself to a value in memory
and that name cannot bind itself to a different value now (unlike variables).
The value can be mutated (if mutable) but the name cannot bind to a different
value once it has bind itself to a value.
On Wed, 26 May 2021 at 12:55, Shreyan Avigyan wrote:
>
> > What's a const *ptr and a const *ptr const?
>
> In C, a const pointer means a pointer that can only point to one value while
> const pointer const means a pointer that can only point to one constant value.
Python has names that bind to v
On 5/26/21 7:40 AM, Steven D'Aprano wrote:
> On Wed, May 26, 2021 at 11:26:17AM -, Shreyan Avigyan wrote:
>> Reply to Richard Damon:
>>
>> The values can be changed. It can be mutated (if mutable). This idea
>> suggests we can't reassign anything to the name. Suppose,
>>
>> constant x = ["List
> What's a const *ptr and a const *ptr const?
In C, a const pointer means a pointer that can only point to one value while
const pointer const means a pointer that can only point to one constant value.
___
Python-ideas mailing list -- python-ideas@pytho
Reply to Steven -
Sorry for creating confusions.
1. Leave debugging. As I said that's not a good argument for the existence of
constants.
2. "Constants doesn't mean we can't reassign the name to a different value."
was differently intended. I was trying to say that we should *treat* it like
On Wed, May 26, 2021 at 11:26:17AM -, Shreyan Avigyan wrote:
> Reply to Richard Damon:
>
> The values can be changed. It can be mutated (if mutable). This idea suggests
> we can't reassign anything to the name. Suppose,
>
> constant x = ["List"]
> x.append("something") # OK
>
> x = [] # Err
Shreyan, you have probably spent hundreds, maybe thousands of words to
avoid giving straightforward, direct, explicit answers to the questions,
preferring to force us to guess what you mean from vague analogies.
Please stop. Please give an explicit description of the functional
requirements of
On Wed, May 26, 2021 at 9:20 PM Richard Damon wrote:
>
> On 5/26/21 5:55 AM, Shreyan Avigyan wrote:
> > Reply to Steven -
> >
> > Literals mean 10 or 20. Can you assign something to literal? No. But you
> > can assign something to a variable to point to another value. That's why I
> > said const
Reply to Richard Damon:
The values can be changed. It can be mutated (if mutable). This idea suggests
we can't reassign anything to the name. Suppose,
constant x = ["List"]
x.append("something") # OK
x = [] # Error
Think of it as a const *ptr. Don't think of it as const *ptr const.
___
On 5/26/21 5:55 AM, Shreyan Avigyan wrote:
> Reply to Steven -
>
> Literals mean 10 or 20. Can you assign something to literal? No. But you can
> assign something to a variable to point to another value. That's why I said
> constants should behave like literals. Point is constants are names bind
Reply to Steven -
Literals mean 10 or 20. Can you assign something to literal? No. But you can
assign something to a variable to point to another value. That's why I said
constants should behave like literals. Point is constants are names bind to a
value. We can change the value but not the nam
On Wed, May 26, 2021 at 07:39:54AM -, Shreyan Avigyan wrote:
> Chris wrote:
> > There are many arguments in favour of constants, but this one strikes
> > me as particularly weak.
>
> That's not exactly how I meant it. I was telling that while constant
> should be there it should not be used a
On Wed, May 26, 2021 at 07:14:41AM -, Shreyan Avigyan wrote:
> Reply to Steven D'Aprano:
>
> > But you've said that you want constants to be capable of being rebound
> > to a new value. So your constants are identical to variables.
>
> No, not at all. Actually to be clear, constants are supp
On Wed, May 26, 2021 at 07:25:09AM -, Shreyan Avigyan wrote:
> 2) Class member constants - Constants should be used only for avoiding
> from being overridden.
But you said that constants can be overriden and the name rebound.
Is your proposal only to add a keyword "constant" that does absol
Chris wrote:
> There are many arguments in favour of constants, but this one strikes
> me as particularly weak.
That's not exactly how I meant it. I was telling that while constant should be
there it should not be used as "Use constants everywhere". I actually believe
the main reason for constan
On Wed, May 26, 2021 at 5:26 PM Shreyan Avigyan
wrote:
>
> Reply to Chris:
>
> There are two things I want to say about constants :-
> 1) Global-Local Constants - The ALL_CAPS convention variables should become
> constant.
> 2) Class member constants - Constants should be used only for avoiding f
Reply to Chris:
There are two things I want to say about constants :-
1) Global-Local Constants - The ALL_CAPS convention variables should become
constant.
2) Class member constants - Constants should be used only for avoiding from
being overridden. It should not be used as "We have a class. Use
Reply to Steven D'Aprano:
> But you've said that you want constants to be capable of being rebound
> to a new value. So your constants are identical to variables.
No, not at all. Actually to be clear, constants are supposed to behave like
literals but in implementation they are nothing more tha
On Wed, May 26, 2021 at 11:57 AM Steven D'Aprano wrote:
>
> On Wed, May 26, 2021 at 02:09:58AM +1000, Chris Angelico wrote:
>
> > Remember: The thing that you declare Final will, some day, need to be
> > changed. Probably as part of your own test suite (you do have one of
> > those, right?). With
On 26/05/2021 02:32, Steven D'Aprano wrote:
`math.pi` says hello.
The existence of math.pi and the fact that probably nobody ever has
introduced a bug into their code by carelessly assigning another value
to it suggests strongly that the benefit of language enforced constants
is not high.
B
On Tue, May 25, 2021 at 9:36 PM Steven D'Aprano wrote:
> > ALLCAPS means "this value should only be set once and not changed."
>
> `math.pi` says hello.
>
(U ⇒ O) ⊬ (¬U ⇒ ¬O)
The existence of math.pi and the fact that probably nobody ever has
> introduced a bug into their code by carelessly ass
On Tue, May 25, 2021 at 06:09:29PM -, Shreyan Avigyan wrote:
> I actually suggest a different kind of Constant in Python not the
> classical const we see in Java or C/C++.
>
> Constants doesn't mean we can't reassign the name to a different
> value.
So... constants aren't constant? They're
On Wed, May 26, 2021 at 02:09:58AM +1000, Chris Angelico wrote:
> Remember: The thing that you declare Final will, some day, need to be
> changed. Probably as part of your own test suite (you do have one of
> those, right?). With MyPy, you could tell it not to validate that
> line, or that file. W
On Tue, May 25, 2021 at 11:37:06AM -0400, David Mertz wrote:
> We've already had all of this since long before typing.py existed.
>
> ALLCAPS means "this value should only be set once and not changed."
`math.pi` says hello.
The existence of math.pi and the fact that probably nobody ever has
int
On Tue, May 25, 2021 at 04:27:41PM +0100, Stestagg wrote:
> On Tue, May 25, 2021 at 4:02 PM Ethan Furman wrote:
> > Optional typing != core Python.
> >
> >
> I mean, it's part of the standard library, so we could argue on personal
> definitions of 'core Python', but it would be more useful to wor
On 5/25/21 8:33 AM, Damian Shaw wrote:
> On Tue, May 25, 2021 at 11:02 AM Ethan Furman wrote:
>> On 5/25/21 5:23 AM, Chris Angelico wrote:
>>> On Tue, May 25, 2021 at 9:55 PM Shreyan Avigyan wrote:
The proposed syntax is as follows,
constant x = 10
constant y = ["List"]
>>>
I actually suggest a different kind of Constant in Python not the classical
const we see in Java or C/C++.
Constants doesn't mean we can't reassign the name to a different value.
Constants behave like literals. They are sort of literals actually. We
reference a value by a name. Variable is just
Greetings,
On Tue, May 25, 2021 at 8:13 PM Chris Angelico wrote:
> Remember: The thing that you declare Final will, some day, need to be
> changed. Probably as part of your own test suite (you do have one of
> those, right?). With MyPy, you could tell it not to validate that
> line, or that fil
On Tue, May 25, 2021 at 5:24 PM Shreyan Avigyan
wrote:
> Reply to Chris:
>
> Wait. Deployment? Before deploying we *run the code* at least once and
> then we get the errors. And I'm not sure but type checking is ok but
> sometimes enforcing is a better option. Why? Mypy or typecheckers have to
>
Reply to Chris:
Wait. Deployment? Before deploying we *run the code* at least once and then we
get the errors. And I'm not sure but type checking is ok but sometimes
enforcing is a better option. Why? Mypy or typecheckers have to be run manually
and they are also not part of the stdlib. Enforci
On Wed, May 26, 2021 at 2:00 AM Shreyan Avigyan
wrote:
>
> I'm aware that Python is a "Consenting Adults" language but I think Python
> should provide this functionality at least. This idea doesn't actually make
> it like Java. Because why should we mess around with that member anyway.
> Someti
I'm aware that Python is a "Consenting Adults" language but I think Python
should provide this functionality at least. This idea doesn't actually make it
like Java. Because why should we mess around with that member anyway. Sometimes
we want to provide members that users must use but mustn't cha
if you want it for debugging, then static type checkers can do that for you.
2) This would allow another branch of OOP programming to enter Python.
> Read-only member variables. There are Private members in Java and C++.
Then use Java or C++ -- the benefits of Python are not primarily
the syntax
On Tue, May 25, 2021 at 4:41 PM Shreyan Avigyan
wrote:
> First it would seem useless or not necessary but soon this becomes clear.
> 1) It would help programmers debug their code easily and help them find out
> that the bug's not in the constant,
A reasonable type checker will detect cases wher
First it would seem useless or not necessary but soon this becomes clear. 1) It
would help programmers debug their code easily and help them find out that the
bug's not in the constant, that's for sure. 2) This would allow another branch
of OOP programming to enter Python. Read-only member varia
We've already had all of this since long before typing.py existed.
ALLCAPS means "this value should only be set once and not changed."
_underscore means "this is not part of public API, and it may change in
next version.
__double_under means "this is SERIOUSLY not something you should muck with,
It's still a Python feature even if it's not a language feature, it's well
defined by PEP and any type checker wanting to implement type hinting to
spec must include it.
Further type hinting allows developers who want to use a Constant / Final
feature get the benefit already from type hinting, whe
On Tue, May 25, 2021 at 4:02 PM Ethan Furman wrote:
> On 5/25/21 5:23 AM, Chris Angelico wrote:
> > On Tue, May 25, 2021 at 9:55 PM Shreyan Avigyan wrote:
>
> >> The proposed syntax is as follows,
> >>
> >> constant x = 10
> >> constant y = ["List"]
> >> constant z: str = "Hi"
> >
> > htt
Ethan:
> Optional typing != core Python.
Exactly
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
h
On 5/25/21 5:23 AM, Chris Angelico wrote:
> On Tue, May 25, 2021 at 9:55 PM Shreyan Avigyan wrote:
>> The proposed syntax is as follows,
>>
>> constant x = 10
>> constant y = ["List"]
>> constant z: str = "Hi"
>
> https://docs.python.org/3/library/typing.html#typing.Final
>
> Already exists :)
O
Yes I'm aware of that. That's for typing. I'm talking about implementing it in
Python itself not Python typing.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org
On Tue, May 25, 2021 at 9:55 PM Shreyan Avigyan
wrote:
>
> I posted my previous idea regarding this on the mailing list. This idea is a
> little different. This idea suggests introducing constant name bindings. This
> is similar to const pointer in C/C++. Once a name has been assigned to a data
70 matches
Mail list logo