Constants in CLIPS

Constants

Python supports constants of string, boolean, and numeric values by convention. Although Python does not have a built-in constant enforcement, we typically use uppercase variable names to indicate they should not change.

import math

# Declaring a constant value
S = "constant"

def main():
    print(S)

    # A constant can appear anywhere a variable statement can.
    N = 500000000

    # Constant expressions perform arithmetic with arbitrary precision.
    D = 3e20 / N
    print(D)

    # A numeric constant has no type until it’s given one.
    print(int(D))

    # A number can be given a type by using it in a context that requires one.
    # For example, here math.sin expects a float.
    print(math.sin(N))

if __name__ == "__main__":
    main()

To run the program, save the code in a file named constant.py and use the Python interpreter.

$ python constant.py
constant
6e+11
600000000000
-0.28470407323754404

In Python, uppercase variable names are used to indicate that a variable should be treated as a constant. Though it does not enforce these as true constants, it helps in understanding the code and maintaining consistency across the codebase.

查看推荐产品

Comments powered by Disqus