Just a mistype: I forgot to add @ before property decorator. Lost approx 1 hour to find out what went wrong. Here is an example:
class Woo(object):
@property
def success(self):
return True
property
def failure(self):
return False
if __name__ == '__main__':
woo = Woo()
print(f'success={woo.success}')
print(f'failure={woo.failure}')
print(f'It\'s fine to call failure()={woo.failure()}')
The result is:
success=True
failure=<bound method Woo.failure of <__main__.Woo object at 0x0000016050D4F6D8>>
It's fine to call failure()=False
I'm wondering why Python compiler allows "naked decorator" (e.g. property without decorator prefix @) in syntax and what does it mean semantically.
It's perfectly legal to have a symbol floating around without any purpose, similar to this:
a = 1
a # Perfectly legal, although a thorough linter* will cause a warning
In your example, it's just a top-level reference to the property class that isn't used for anything, similar to if you had:
class MyClass:
a = 1
a # Legal, but causes a warning since it has no purpose
def meth(self):
pass
1 # Also legal but useless
def another_meth(self):
pass
It isn't being treated as a decorator; it's being treated as a plain reference to the property class in a context where it isn't used for anything.
* Pycharm gives the warning Statement seems to have no effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With