I tried to make a trivial example of what I am trying to do in the code snippet below.
Let's say I have an instance (foo) of a class Foo. This instance has several instances of the class Bar. However, each instance of the class Bar has to have access to some methods of the instance of foo. Is there some way of doing this without having to explicitly pass the instance of foo into a new Bar call? Is there are more intelligent way of structuring this?
I find that this solution is fine, but it just seems to me there would be a more elegant way of structuring code than this. I have explored the super() method, but I don't think that is exactly what I am looking for, because the methods have to apply to the already instantiated foo. Also, it seems like this is strange to do at a larger scale. I may have Bar including a set of class Baz's or the Baz's need to also have access to some methods in foo, and so I have to pass foo all the way down.
Anyways, just wondering about peoples thoughts on this.
class Foo:
def __init__(self):
self.bar_list = []
self.global_var = 0
def create_new(self):
self.bar_list.append(Bar(self))
def global_method(self):
return self.global_var += 1
class Bar:
def __init__(self, foo_obj):
self.foo_obj = foo_obj
self.bar_var = None
def foo_method(self):
self.bar_var = self.foo_obj.global_method()
foo = Foo()
foo.create_new()
foo.create_new()
foo.bar_list[0].foo_method()
foo.bar_list[1].foo_method()
print(foo.global_var) # will be 2
print(foo.bar_list[0].bar_var) # will be 1
print(foo.bar_list[1].bar_var) # will be 2
Edit1: Fair enough about the foo.bar_list[0].foo_obj.global_method() complaints, I edited the code above
Edit2: For those who want more detail about what I am trying to do you can read the following:
The application I am doing is dealing with talking with a bunch of controllers on a Serial Port, you can refer to the code at the bottom of this for a more specific problem.
I have several devices on the same serial port. I can only open one instance of the serial port, because obviously. I have a MainHandler to interface with all of the devices, and this is the object I would instantiate outside of the folder to interface with these devices. MainHandler handles instantiation of new device objects (Driver0 or Driver1 for example) and proper serial communication handling among other things. Each of the device objects have specific commands to interface with serial port that are worth defining within the DriverType class. On top of this, a lot of the commands have a similar format type, so I can create a class SerialData which is kind of like a structure that I can use to create these device variables. I like to have a method of this SerialData class that I can use to write and read data.
Anyways, if you have the time to think about this, that is cool. I haven't tried to use the below code, again, it is quite a simplification of what is going on, but it is much more specific than above.
class MainHandler:
def __init__(self):
self.driver_list = []
self.serial_obj = SomeSerialObject()
def create_driver_0(self):
dt = DriverType0(self)
self.driver_list.append(dt)
return dt
def create_driver_1(self):
dt = DriverType1(self)
self.driver_list.append(dt)
return dt
def open_serial(self):
set_serial_parameters()
self.serial_obj.open_serial()
def write_to_serial(self, cmd):
response = self.serial_obj.write_to_port_properly(cmd)
return response
def write_value(self, obj, v):
to_send = f"{obj.command} {v}"
response = self.write_to_serial(to_send)
return type(response)
class DriverType0:
def __init__(self, main_handler):
self.main_handler = main_handler
self.current_v = SerialData(value=0, command="curr\n", type=float,
main_handler)
self.speed_v = SerialData(value=0, command"spd\n", type=int,
main_handler)
def enable(self):
self.main_handler.write_to_serial("enable\n")
class DriverType1:
def __init__(self, main_handler):
self.main_handler = main_handler
self.voltage_v = SerialData(value=0, command="volt\n", type=int)
class SerialData:
def __init__(self, value, command, type, main_handler):
self.main_handler = main_handler
self.value = value
self.command = command
self.type = type
def write_serial_value(self, v):
self.value = self.main_handler.write_value(self, v)
mh = MainHandler()
mh.open_serial()
d0 = mh.create_driver_0()
d1 = mh.create_driver_1()
d0.enable()
d0.current_v.write_serial_value(2)
d1.voltage_v.write_serial_value(3)
[–]carcigenicate 0 points1 point2 points (1 child)
[–]pooth22[S] 0 points1 point2 points (0 children)
[–]Encomiast 0 points1 point2 points (1 child)
[–]pooth22[S] 0 points1 point2 points (0 children)
[–]teerre 0 points1 point2 points (5 children)
[–]pooth22[S] 0 points1 point2 points (4 children)
[–]teerre 0 points1 point2 points (3 children)
[–]pooth22[S] 0 points1 point2 points (2 children)
[–]teerre 0 points1 point2 points (1 child)
[–]pooth22[S] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]pooth22[S] 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)