In Swift we can do the following, at any scope (including inside another function):
var X:Int = 3
var twiceX:Int{
return 2*X
}
print(twiceX) //6
This means we can call a getter function without using the "()" syntax. Any function that takes no argument and return one value can also be implemented as a computer property. Additionally, it is also possible to provide a setter function.
I see that it is possible to make computed properties that belong to classes, using @property declarator, but I see no way to make this a global one. I would expect anything possible at class scope, should be possible at global scope.
Are there global computed properties in Python?
Remarks: I understand that it is mostly a synthetic sugar, that eliminates one pair of ‘()’ every call. However, there are cases where a property is more intuitive than a function. An example is:
queue = [1,4,7]
def current():
return queue[0]
This is used for the same reason that computed properties is seen in classes: as a presentation of a property, without storing the same information twice.
No. Variable lookup and attribute lookup are completely separate mechanisms in Python. What is possible in one is not necessarily possible in the other, and in this case, there is no equivalent of property for variables.
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