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.
精彩评论