Hello everyone!
Have you ever wished that instead of having to have your users use annoying function names like getSelection() or setSelection(), they could just use a property that did it all for them? No more user function calling. Welcome to the future, or at least the unknown past.
Here is a demo of the built-in property function:
class Vector:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
def __repr__(self):
return "(%.2f, %.2f, %.2f)"%(self.x, self.y, self.z)
def get_mag(self):
return (self.x**2+self.y**2+self.z**2)
magnitude = property(get_mag)
vec = Vector(1,2,3)
print('vec initialized at:')
print(vec)
print('magnitude=', vec.magnitude, '\n') # == 14
vec.x = 10
print('vec.x = 10')
print(vec)
print('magnitude=', vec.magnitude, '\n') # == 113
vec.y = -5
print('vec.y = -5')
print(vec)
print('magnitude=', vec.magnitude, '\n') # == 134
See more at this AMAZING tutorial!
http://adam.gomaa.us/blog/2008/aug/11/the-python-property-builtin/
I’m glad I could share this with you, and sorry for not posting in so long.
Happy coding!
-Sunjay Varma