Python defines constants

Source: Internet
Author: User

Constants are fixed values that cannot be modified once initialized. A const reserved word is used in C + + to specify constants, while Python does not define a reserved word for constants. But Python is a powerful language, and you can define a constant class to implement the function of a constant.

const.py

1 #-*-coding:utf-8-*-2 3 class_const:4     classConsterror (TypeError):Pass5 6     def __setattr__(self, Key, value):7         #self.__dict__8         ifSelf.__dict__. Has_key (key):9             RaiseSelf. Consterror,"constant reassignment error!"TenSelf.__dict__[Key] =value One  A ImportSYS -  -sys.modules[__name__] = _const ()

Call

test.py, so it can be used, this value cannot be changed

Import= 10000

Python defines constants

Related Article

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.