python @property装饰器

python @property装饰器

 

class Goods:
    name = '笔记本'

    @property
    def price(self):
        print('print价格是:0.01元!!!')
        return 'return价格是:0.02元!!!'

obj = Goods()
print('*************0000*****************')
obj.price
print('*************111************')
Goods().price
print('*************222*****************')
print(obj.price)
print(Goods().price)
print('*************333*****************')
obj.price()
print('*************444*****************')
Goods().price()
print('*************555*****************')
print(obj.price())
print(Goods().price())

输出:

*************0000*****************
  File "E:/python_projects/practises/practise20191116/p20191124.py", line 100, in <module>
print价格是:0.01元!!!
    obj.price()
*************111************
TypeError: 'str' object is not callable
print价格是:0.01元!!!
*************222*****************
print价格是:0.01元!!!
return价格是:0.02元!!!
print价格是:0.01元!!!
return价格是:0.02元!!!
*************333*****************
print价格是:0.01元!!!

Process finished with exit code 1
原文地址:https://www.cnblogs.com/111testing/p/11922095.html