python中的常量

class MyMath(object):
  def __init__(self):
  self.__PI = "3.1415926"

  @property
  def PI(self):
  return self.__PI

math = MyMath()
print(math.PI)

注:在__init__方法中初始化一个私有属性,提供一个得到这个私有属性的方法,但是我们为了让常量更直观,在方法上加入装饰器@property

 

posted @ 2018-05-10 18:09  夫之  阅读(78)  评论(0)    收藏  举报