/ home / Programming / Python /

'const'

Const basically makes data "Read-Only". Its an important feature to help prevent developers from changing data they were not supposed to, and to informing other developers that data marked const will not change.

Python Qt/Cpp
  1. >>> from typing import Final
  2. >>> sneed: Final = "feed"
  3. >>> sneed
  4. 'feed'
  5. >>> sneed = "seed"
  6. >>> sneed
  7. 'seed'
  1. /* west const */
  2. const QString sneed("feed");
  3. sneed = "seed"; // Error
  4. /* east const */
  5. QString const chuck("seed");
  6. chuck = "feed"; // Error

Read as:
  • [ Something identified as ] 'sneed' [which may or may not already exist]
  • finally [ but not really finally ] equals "feed" [ implying that sneed is a string type ].
  • A [ new ] constant string 'sneed'
  • is [ initialized as ] "feed".

Benefits:
  • mypy will tell you that this variable 'shouldnt' be reassigned.
  • Code can not be compiled if you try to overwrite sneed.
  • Standard practice.
  • Part of the language.

Drawbacks:
  • You have to install mypy.
  • Variable is not readonly.
  • `sneed: Final = "feed"` can colliqually referred to as an "east const".
  • Not standard practice.
  • Not part of the language.
  • None.

Final Thoughts