+ 1

constants in python

I have read on internet that in python constants are variables which can't be changed. I have created a module const.py and created a constant p=10 in it. When I have imported it in another module and tried to change it by const.p=9 ,it is working. How ?

10th May 2019, 12:29 PM
harshit
harshit - avatar
5 Réponses
+ 5
In Python you can't define constants like in other languages.
10th May 2019, 1:39 PM
HonFu
HonFu - avatar
+ 5
There are no constants in Python, only workarounds if you truly feel that it is necessary. You can signal that a value is a constant by using all upper letters for the identifier. For a workaround you can use @property (in classes). Here is a code explaining it a bit: https://code.sololearn.com/cKcc6g3Q6max/#py
10th May 2019, 2:05 PM
Thoq!
Thoq! - avatar
+ 3
The information is misleading. In Python you have no real constants. They are just using capitals to *suggest* a constant. A lot in Python follows this idea: Assuming the people stick to conventions and just *know* they shouldn't change a VALUE. But you can.
10th May 2019, 1:59 PM
HonFu
HonFu - avatar
+ 1
Anna ~ swim ~ HonFu please see this
10th May 2019, 1:24 PM
harshit
harshit - avatar
10th May 2019, 1:51 PM
harshit
harshit - avatar