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?
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 theNewConnectionconstructor 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: