Domanda

I was wondering if a value that is defined by the user at the start of a program, and not modified by the program, is considered a constant or a variable. I know that a constant is a word/letter that holds a value that is not changed during the execution of a program (EG: pi or e), and a variable is also a word/letter that holds a value during the execution of a program, but it's value can be changed.

For example, in the code below, would the identifier interest be considered a variable or a constant?

#Python 3
#Program to calculate the interest gained in a bank account after 1 year

interest=float(input("Interest rate: "))
while True:
    value=float(input("Account value: "))
    value=(value * interest)-value
    print(value)
    print()
È stato utile?

Soluzione

Variables are not called variables because their values may be changed, but because their value isn't known in advance – when writing the code, they are placeholders for values that will be determined later.

In contrast, the value of a constant is known when the program is written. Giving that known value a name is a matter of convenience and maintainability.

Your interest would therefore be characterized as a variable, since it is only determined at runtime.

Python does not have a built-in concept of constants. Constants are just module-level variables with the convention that they will not be changed. According to the PEP-8 styleguide, they should use all-uppercase letters, e.g.:

INTEREST = 0.03

Altri suggerimenti

A constant, in any programming language, is an identifier whose value is not meant to be changed once it is set. It does not matter when this value is learned so long as the constant can not be accessed with anything but this one value.

If you wish to distinguish between types of constants based on when their value is known the terms are:

  • Compile-time (statically-valued) constants
  • Run-time (dynamically-valued) constants

Values that are defined at compile time directly in the source are called literals. 2 is a literal.

How well constants are protected from accidental change varies from language to language. A common naming convention to signal the intent not to change the value to capitalize the name.

Python has a tradition of using naming conventions rather than keywords to signal what keywords would otherwise enforce. There is no const keyword just like there is no private keyword. Instead they use CAPITALS_SEPERATED_BY_UNDERSCORES and __PRIVATES_PRECEDED_BY_UNDERSCORES.

While Python has no const keyword to protect a constant from change there are ways to simulate the protection with getters and such. If you simply don't trust your fellow programmer to respect what you signal with the naming convention. Much like in Java, where the determined can defeat these protections using reflection, understand that nothing will perfectly protect this value. Be sure the naming convention is really insufficient for your needs before constructing your own protections.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
scroll top