A really, really stupid python question

python

#1

I have an object that I know is a class instance attribute, say:
someClassInstance.io.inputs
//someClassInstance.io.inputs is a tuple//
How can I get from that attribute back to the instance itself? If I receive this as an argument or whatever, what lets me get back to the parent class of the attribute?

Thank you in advance


#2

You’d really have to include the ref in the return type of the the property (you’d almost certainly hide this behind a property decorator).

class Example(object):
      def __init__(self, initial = (1,2))
            self._value = initial

      @property
       def value(self):
              return (self._value, self)

The hard part, from a design point of view. is that the property is assymetrical – you don’t want to allow people to set the owner portion of the value, but you want it returned.

In general, you knew the class instance when you asked for the property, so why not just hang on to the reference there? Or turn the property into a method, so you keep the instance all the time and then call the method only when you want the value?


#3

That makes a lot of sense, thanks - I need to take another look at how I pass around information. But it still surprises me that there is no simple way to find an object’s parent when it’s declared as part of another object. Surely if the attribute cannot exist without the instance, shouldn’t each have some information about the other?


#4

The attribute can exist on its own, though; if the value is a float or an int, there’s nothing to distinguish it from from any other example of the same type.