Implementing constant (Const) functionality in Python _python

Source: Internet
Author: User

The Python language itself does not provide a const, but it often encounters the need to use const in the actual development, because the language itself does not have this expense, therefore needs to use some techniques to achieve this function

Define the Const class as follows

Copy Code code as follows:

Import Sys

Class Const (object):
Class Consterror (typeexception): Pass
def __setattr__ (self, Key, value):
If Self.__dict__.has_key (key):
Raise self. Consterror, "changing const.%s"% key
Else
Self.__dict__[key] = value

def __getattr__ (self, key):
If Self.__dict__.has_key (key):
Return Self.key
Else
Return None

SYS.MODULES[__NAME__] = Const ()


Using Sys.modules[name], you can get a module object that gets the properties of the module, using the Sys.modules to inject a const object into the system dictionary to implement the import Const actually gets the functionality of a const instance, as described in the Sys.module document
Copy Code code as follows:

Sys.modules
This is a dictionary which maps module names to modules which have already been. This can is manipulated to force reloading of modules and other tricks. Note which removing a module from this dictionary is not the same as calling Reload () on the corresponding module object.

Sys.modules[name] = const () This statement replaces the const in the list of modules loaded by the system with the const (), that is, a const instance

In this way, the constants to be used throughout the project should be defined in a file, as follows

Copy Code code as follows:

From project.utils Import const

Const. Mail_proto_imap = ' IMAP '
Const. Mail_proto_gmail = ' GMAIL '
Const. Mail_proto_hotmail = ' HOTMAIL '
Const. Mail_proto_eas = ' EAS '
Const. mail_proto_ews = ' EWS '


Here we first need to explain the difference between the import module in Python and the From module import

1.import module simply adds the name of module to the local dictionary of the destination file and does not need to interpret the module
2.from Module Import XXX requires the module to be interpreted and loaded into memory, then the corresponding part is added to the local dictionary of the target file.
Code in the 3.python module is only executed once when it is first import

From project.utils import Const, a sys.modules[name] = const () occurs when the const module is already loaded into memory, and the const object is already in the system dictionary, which can then be used with the const instance

When you need to use constant values in other files, you invoke the following method

Copy Code code as follows:

From project.apps.project_consts Import const

Print Const. Mail_proto_imap

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.