What exactly constitutes a constant in Python?

前端 未结 2 1604
执笔经年
执笔经年 2021-01-16 05:57

PEP 8 prescribes that

Constants are usually defined on a module level and written in all capital letters with underscores separating words. Example

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-16 06:09

    For the most part in python the PEP8 naming convention for constants is just there to help any programers who are reading the code know not to change that particular "constant" In other languages like C it actually can reduce the time it takes to execute code.

    See this post on The Benefits of Constants.

    A constant is just a value that never changes.

    In OOP, it is useful to declare private or public constants but for python there is no functional separation from "Constants" and regular variables.

    One more thing in other programming languages that support true constants if you declare a value as a constant then that value cannot change not even if you try to reassign that value later. In Python, however, there are no true constants, so we use the naming convention to help us remember not to change that value.

提交回复
热议问题