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

猜你喜欢

转载自www.cnblogs.com/zrf-516/p/9021000.html