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 to a > value. We can change the value but not the name to point to a different > value. Simply think "const type *pointer" not "type *pointer const". > > And by debugging I meant it would immediately be detectable if someone tries > to change the constant or not. (Not a good argument in constant's favor > though)
I think the biggest problem is that basically, by definition, to actually enforce this requires a attribute lookup on EVERY rebinding or mutating operation. One stated goal was that constant mylist = [1, 2, 3] was to generate an error if you do mylist.append(4) but that also means we need to catch yourlist = mylist yourlist.append(4) and since that binding could happen on a cross module function call, every mutating operation needs a run-time check to see if this object has been bound to a constant. We also need to test everytime we bind/rebind a name, as the creation of that constant binding might be hidden behind mechanisms that we can't determine at compile time. Things like can another module inject a constant into our module? If so, we need to check every name binding to see if that name has been made a constant. This can add up to a noticable run time cost. Using the alternative of type hints says that the checker can warn us of many of the violations, and you have the OPTION of turning on run time checking if you think you have a problem (or you can just choose to always turn it on and pay the cost). -- Richard Damon _______________________________________________ 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 https://mail.python.org/archives/list/python-ideas@python.org/message/RSFAX56IETA4UZ7WOQGOFEHZZ6SRQWOZ/ Code of Conduct: http://python.org/psf/codeofconduct/