开发者

Languages without built-in support for constants [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate开发者_运维知识库, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

Why does neither Perl nor Python have support for constants? Is it just considered a marginal feature that just no one meant to implement, or is there maybe something more behind it?


Python doesn't support constants because the idea is against its philosophy: we're all consenting adults here. That is, Python doesn't forbid changing things through the software; instead, it merely indicates that you shouldn't change things and may stop working if you do.

To indicate a constant in a Python script one normally makes it a module-level variable in ALL_CAPS.

(Another common example is 'private' members of a class, normally marked with a single leading underscore, which are private only by convention and work the same as ordinary attributes.)


You cannot declare a variable or value as constant similar to any other languages (like JAVA or C). Just don't change it.

You can differentiate normal and Constant variables by using proper naming conventions. i.e declare the constant as all caps

class Foo(object):
  CONST_NAME = "Name"

#or just
CONST_NAME = "Name"

though, if you need constants to be work on python. this links Constants in Python, Constants, would be helpful


Create a "constants" class in Perl or Python, keeping your constants in this class. Import the class in your Perl or Python script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜