0

I am C++ programmer and new to python.I found many good things in Python but one thing that was strange to me that there is no way to make variable constant in Python Here is my code:

def main():
   a = 10
if __name__ == "__main__":
      main()

how can I make a constant. Thanks for your help.

Algirdas Preidžius
  • 1,769
  • 3
  • 14
  • 17
  • 1
    Just don't change it? Java doesn't have `const` either, although `final` can proxy for primitives. – Bathsheba Dec 08 '17 at 14:53
  • 4
    How is your question related to C++? – Algirdas Preidžius Dec 08 '17 at 14:53
  • 1
    No such thing. There's also no private, public and all that stuff in python either. Python doesn't have such neat features. – mroman Dec 08 '17 at 14:54
  • You can define a function and return your constant! `def Const_Value(): return 10` – Hossein Dec 08 '17 at 14:56
  • @mroman `_` and `__` want to have a word with you. – Revolver_Ocelot Dec 08 '17 at 14:57
  • 1
    Python doesn't actually have C-like variables, as explained in [Other languages have "variables", Python has "names"](http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables). For a fuller discussion of this important topic, from a slightly different perspective, please see [Facts and myths about Python names and values](http://nedbatchelder.com/text/names.html), which was written by SO veteran Ned Batchelder. – PM 2Ring Dec 08 '17 at 14:57
  • 1
    @Revolver_Ocelot `__` doesn't make them private. It's just name mangling. – mroman Dec 08 '17 at 15:01
  • 1
    @Revolver_Ocelot Prepending a single underscore to a name to mark it as private is merely a stylistic convention, the language does nothing to enforce privacy of such names in any way. Prepending a double underscore (with at most a single trailing underscore) invokes Python's [name-mangling](https://docs.python.org/3/tutorial/classes.html#private-variables) machinery, which _does_ give a form of privacy, but it's primary purpose is to prevent name clashes, and it doesn't really make the name private since the name mangling process is rather basic. – PM 2Ring Dec 08 '17 at 15:03
  • @mroman in C++ `private` does not make fields private, it just blocks naive acccess. In Java `private final` field an object in a running program can stil be changed. Name mangling does prevent _accidental_ access outside of your class. It is almost _impossible_ to prevent intentional access to private data anyway. – Revolver_Ocelot Dec 08 '17 at 15:05
  • @Revolver_Ocelot I don't know C++ but yes, using Reflection you can certainly make a private method in Java accessible but obviously given enough freedoms you can mess with anything at runtime but the point is whether it's correctly checked at compile time, which it isn't in python. If I define a method `__foo` in `C` and _accidentally_ write `my_c._C__foo` I can accidentally call it without python warning or complaining about it, thus there's in essence absolutely no guarantee you'd expect from something that is truly `private`. – mroman Dec 08 '17 at 15:09
  • @mroman _"accidentally write `my_c._C__foo`"_ I find it as plausible as accidentally writing `*reinterpret_cast(reinterpret_cast(&my_c.public_member) + sizeof(my_c.public_member)) = 0xDEFACED` in C++. – Revolver_Ocelot Dec 08 '17 at 15:16
  • @Revolver_Ocelot as a non-C++ guy that looks like undefined behaviour anyway and thus shouldn't be compilable (even though it does, because C/C++ compilers sometimes allow it, sometimes silently optimize it away and whatnot.. `my_c._C_foo` however is perfectly legit. – mroman Dec 08 '17 at 15:44

0 Answers0