0

In a Python software I am working on, I have a number of objects that need to be able to be 'connected' to other objects (the term connected here referring to the fact that it just needs to point to the object it is connected to).

I have thought to create an connection class, which can create this link and store information about the properties of the connection. As shown below, when I create a new object, I have a method that allows me to add a new connection with a certain type to it.

class ConnectableObject()
    def addNewConnection(self, connectiontype)
        self.connections.append(NewConnection(connectiontype))

The idea is that each object should have their own connection type... and that these are connectable. i.e Object A has a connection object associated with it, Object B has a connection object associated with it, and to connection the two objects I just connect their connections. That allows me to store information about each connection in the connection objects. Once this connection object is instantiated I'd like to store in the connection object 'who' created it, i.e object A... object B. Is there a way in Python to request from an Object what class it was created within?

Also, more generally, is there a cleaner way to implement connections between objects where each object connection can store its own properties?

1 Answer 1

1

Is there a way in Python to request from an Object what class it created within?

There are too many 'connections' in your question, so I'm not sure if my understanding correct, but I think you need to pass ConnectableObjectInstance.__class__ as an argument of the NewConnection constructor and then store it as a property of your connection object.

EDIT:

You don't need to know a name of the object instance as it can be referenced via self. Also you probably don't need to store class as a connection property, but a reference to an object. Something like that:

>>> class Connection(object):
...  def __init__(self, parent):
...   self.parent = parent
...
>>> class ConnectableObject(object):
...  def __init__(self):
...   pass
...  def connect(self):
...   return Connection(self)
...
>>> conobj = ConnectableObject()
>>> con = conobj.connect()
>>> con.parent
<__main__.ConnectableObject object at 0x01FDEC70>
3
  • I will rethink how to phrase my question to reduce the frequency of 'connections'. Your response does address the question, and I suppose it does reflect a tell don't ask philosophy better than allowing the object to query who created it.
    – Flaminator
    Nov 18, 2012 at 23:35
  • Actually on second thought, how would this work? If say I create an instance of a class and call it foobar... then foobar.__class__ will still return the type (i.e ConnectableObject). This doesn't actually solve the problem, because what I want to know is that the connection 'belongs' to foobar. I can see how this would be an issue though because until you assign the object to a variable (and have fully initialized it), you haven't assigned a name... any ideas?
    – Flaminator
    Nov 19, 2012 at 4:30
  • That does do it, forgot that Python had a self keyword. Thanks!
    – Flaminator
    Nov 20, 2012 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.