Implement constant (const) functionality in Python

Source: Internet
Author: User
The Python language itself does not provide a const, but the actual development often encounters the need to use the const situation, because the language itself does not have this expense, so need to use some techniques to achieve this function

Define the Const class as follows
Copy the 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 and get the properties of the module by using the Sys.modules to inject a const object into the system dictionary to implement the import Const has actually acquired the functionality of a const instance, which is described in the document as follows: Sys.module
Copy CodeThe code is as follows:


Sys.modules
This is a dictionary, the maps module names to modules which has already been loaded. This can is manipulated to force reloading of modules and other tricks. Note that 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 (), which is a const instance

In this way, the constants that the entire project needs to use should be defined in a file, as follows
Copy the 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 you first need to explain the difference between the import module and the From module import in Python

The 1.import module simply adds the module's name to the local dictionary of the target file and does not need to be interpreted by the module
2.from Module Import XXX requires that the module be interpreted and loaded into memory, and then added to the local dictionary of the target file
The code in the 3.python module is executed only once when the first import is made

From project.utils Import const, Sys.modules[name] = Const () occurs, at which time the Const module has been loaded into memory, the system dictionary already has a const object, and the const instance can then be used

When you need to use constant values in other files, you call the following method
Copy the 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.