Can a Python programmer do anything to ensure that a variable's value can never be changed after it's initial assignment? | Sololearn: Learn to code for FREE!
0

Can a Python programmer do anything to ensure that a variable's value can never be changed after it's initial assignment?

Do we have something like const of C in Python?

27th Dec 2018, 8:13 PM
Punyasloka Sahoo
Punyasloka Sahoo - avatar
8 Answers
27th Dec 2018, 8:52 PM
Anna
Anna - avatar
+ 7
HonFu LOL... We typically have a good sense for the strengths and weaknesses of a new developer joining one of my teams before he or she starts working in the code. I've not encountered problems stemming from Python because we simply don't do any serious development with Python. The reasons for not doing any serious development in Python stems from the risks associated with performance, lack of familiarity in a development workflow that we can trust to meet our needs, the uncertainty of Python's direction as a language and community, and the lack of desire to force ourselves to conform to PEP for best practices.
27th Dec 2018, 9:49 PM
David Carroll
David Carroll - avatar
+ 6
https://stackoverflow.com/q/2682745/9132046 Python doesn't have constants but if you want others to know it's a constant you should name it LIKE_THIS https://www.python.org/dev/peps/pep-0008/#constants/
27th Dec 2018, 8:24 PM
Mert Yazıcı
Mert Yazıcı - avatar
+ 5
The whole PEP Pythonic Zen coding thing where everyone just agrees to drink the Kool-aid and follow the conventions and accept fundamental limitations is where I typically struggle with taking this cult, eh hem... language seriously. Just my 2 cents.
27th Dec 2018, 8:53 PM
David Carroll
David Carroll - avatar
+ 4
The only thing that comes to mind is a tuple, which may not be what you are looking for.
27th Dec 2018, 8:59 PM
David Carroll
David Carroll - avatar
+ 2
David Carroll, do you encounter problems stemming from these limitations/liberations in your job life? My first thought would be: What's with the new guy on the team? Who somehow got hired but noone knows yet how good he/she really is?
27th Dec 2018, 9:06 PM
HonFu
HonFu - avatar
+ 1
No, there's no const in Python.
27th Dec 2018, 8:18 PM
HonFu
HonFu - avatar
+ 1
I suppose that's part of the 'we're all grownups here attitude' assuming that a visual cue should be enough to stop you from doing nonsense.
27th Dec 2018, 8:29 PM
HonFu
HonFu - avatar