如何在一个类中定义一些常量,每个对象都可以方便访问这些常量而不用重新构造?
类外访问:
node2:/tmp/20200608#cat Entity.py
class Document():
WELCOME_STR = 'Welcome! The context for this book is {}.'
node2:/tmp/20200608#cat Entity.py
class Document():
WELCOME_STR = 'Welcome! The context for this book is {}.'
node2:/tmp/20200608#
node2:/tmp/20200608#cat t1.py
from Entity import *
a=Document()
print a
print type(a)
print dir(a)
print a.WELCOME_STR
node2:/tmp/20200608#python t1.py
<Entity.Document instance at 0x7f159881dcb0>
<type 'instance'>
['WELCOME_STR', '__doc__', '__module__']
Welcome! The context for this book is {}.