0

If it was a language design choice, what is the explanation for it?

Is there any other way around this to avoid someone or even myself changing a constant value while executing a program, without having to use an immutable data type?

Felipe Alarcon
  • 948
  • 11
  • 19
  • What are you using as a constant? could you use an immutable datastructure? – dm03514 Nov 13 '15 at 20:44
  • Couldn't you just not assign anything to that constant? – Morgan Thrapp Nov 13 '15 at 20:45
  • You mean a Tuple? it feels a bit dirty to me using a tuple for that when I could have an access modifier. – Felipe Alarcon Nov 13 '15 at 20:45
  • 3
    Python isn't C. It has a different data model. Don't think in terms of variables (or constants) that contain a value, think in terms of objects (which may or may not be mutable) that are bound to names. You may find this article helpful: [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Nov 13 '15 at 20:53
  • See also: [Creating constant in Python](http://stackoverflow.com/q/2682745/562769) – Martin Thoma Nov 13 '15 at 20:58
  • Thanks for the links, they are really useful. – Felipe Alarcon Nov 13 '15 at 21:07

1 Answers1

2

Python tends to favor simplicity, and uses name mangling.

In short, the convention is to use underscores for protected/private variables.

Check this as well :

why python does not have access modifier?And what are there alternatives in python?

Edit1:

If you really want to hide them, you could try this:

#constants.py
foo = 1

Then on your project, use:

import constants

And now you have access to them, and "protected".

Community
  • 1
  • 1
mugabits
  • 1,015
  • 1
  • 12
  • 22
  • The underscore doesn't stop me from changing a constant's value. – Felipe Alarcon Nov 13 '15 at 21:05
  • It will not, unless you put all your constant values in their own module. – mugabits Nov 13 '15 at 21:08
  • 1
    @fandressouza: That's where the Python philosophy of "We're all adults here" comes in. The `_` says "You shouldn't." But if you have a legitimate need to do so, Python won't stop you, because it considers you a mature adult, and there are always use cases the original author didn't anticipate. – ShadowRanger Nov 13 '15 at 21:09