python的@property是python的一种装饰器,是用来修饰方法的。
由于python进行属性的定义时,没办法设置私有属性,因此要通过@property的方法来进行设置。这样可以隐藏属性名,让用户进行使用的时候无法随意修改。
class Service(object):
def __init__(self):
self._dailyUrl = "http://daily.com"
self._preUrl = "http://pre.com" #定义属性的名称
@property
def get_daily(self): #方法加入@property后,这个方法相当于一个属性,这个属性可以让用户进行使用,而且用户有没办法随意修改。
return self._dailyUrl
@property
def get_pre(self):
return self._preUrl
service = Service()
#用户进行属性调用的时候,直接调用get_daily即可,而不用知道属性名_daily,因此用户无法更改属性,从而保护了类的属性。
print(service.get_daily) # 加了@property后,可以用调用属性的形式来调用方法,后面不需要加()。
在自动化测试中可放在config.py中进行host的配置。