I am trying to create a client class that is able to be instantiated with connection information and other attributes about how the client is connecting and interacting with a service. The client class would have inner classes that represent objects in the service. These objects could be instantiated in a couple of different ways:
- The outer, client class has a factory method that creates the inner class, like
client.make_someobject()
- this would pass an instance of itself to the new object, so the object would know about and can use the connection information without the caller explicitly passing the connection in. - An existing object in the service can be pulled in by writing
client.SomeObject(some_id)
My question is mostly related to the second scenario. When creating an instance of an inner class directly, without a factory method that can just pass in self, how could I ensure that the new instance of the inner class knows about the attributes of the outer, client class?
Illustrative example:
class Client():
def __init__(self, client_attr):
self.client_attr = client_attr
def make_serviceobject(self):
return ServiceObject._make_serviceobject(self)
class ServiceObject():
def __init__(self, id,client=None):
self.id = id
if client:
self.client = client
# ...
@classmethod
def _make_serviceobject(cls, client):
id = 'some_id'
return cls(id, client=client)
my_client = Client(some_attr)
# now, how can this new ServiceObject know about the my_client attributes and methods?
my_existing_resource = my_client.ServiceObject(some_id)
# I am trying to avoid this:
my_existing_resource = my_client.ServiceObject(some_id, client=my_client)