What I am trying to achieve is something like this:
class object:
def __init__(self):
WidthVariable(self)
print self.width
#Imagine I did this 60frames/1second later
print self.width
#output:
>>0
>>25
What I want happening (as above): When WidthVariable
- a class - is created it adds the variable width
to the object instance. This variable acts like a normal property, but in this particular case it is read-only (only the fget
variable is set). Additionally, the fget
calls a function defined in WidthVariable
which decides what width
to return.
However, I have no idea how to do this! I tried it using normal properties but I found that they only work on classes and not per instance - please note that the code I use should be similar as possible to the above (i.e. only code within the __init__
of WidthVariable
should set the width
variable, nowhere else). Also, self.width
cannot be function, because I do not what to call it like self.width()
, I want self.width
(because it keeps with the rest of the design I have).
To clarify, the complete code would be something like this:
class MyObject:
def __init__(self)
WidthVariable(self)
print self.width
class WidthVariable:
def __init__(self, object)
object.width = property(self.get_width)
def get_width(self):
value = #do_stuff
return value #The Value
#output:
>>25 #Whatever the Value was