class MyMath(object):
def __init__(self):
self.__PI = "3.1415926"
@property
def PI(self):
return self.__PI
math = MyMath()
print(math.PI)
注:在__init__方法中初始化一个私有属性,提供一个得到这个私有属性的方法,但是我们为了让常量更直观,在方法上加入装饰器@property
class MyMath(object):
def __init__(self):
self.__PI = "3.1415926"
@property
def PI(self):
return self.__PI
math = MyMath()
print(math.PI)
注:在__init__方法中初始化一个私有属性,提供一个得到这个私有属性的方法,但是我们为了让常量更直观,在方法上加入装饰器@property