How can I move this class to separate file in python?
Question:
Please have a look at the following code:
class Main:
def __init__(self):
# list to keep track of instances of Sub()
self.sub_list = []
# create instances of Sub()
self.a = Sub()
self.b = Sub()
# append instances to sub_list
self.sub_list.append(self.a)
self.sub_list.append(self.b)
class Sub:
# function to remove the instance from sub_list
def remove_self(self):
x.sub_list.remove(self)
x = Main()
# remove instances of Sub() from sub_list
x.a.remove_self()
x.b.remove_self()
My goal is to have objects with a method to delete themselves from a list in a different class, where they are created.
The above code works for this, but I don’t like that I have to call the method by using x
directly.
The main issue I’d like so solve, however, is that I can’t figure out how to move the class Sub
to a separate file, as I can’t access sub_list
anymore.
Is there a better way to do this?
Side note: I’m working on a project with PyQt, where Main
is my MainWindow and Sub
represents some UI elements that can be dynamically created and also closed. I’d like to keep all functionality within these elements if possible.
Answers:
The ideal solution would be to not have a sub_list
at all. It seems unnecessary since instances of Sub
each get a field to themselves in your example. That being said, if you want to move Sub
to a different file you can do it like so:
sub.py
:
class Sub:
def __init__(self, parent):
self.parent = parent
def remove_self(self):
self.parent.sub_list.remove(self)
And then in your main.py
:
from sub import Sub
class Main:
def __init__(self):
self.sub_list = []
self.a = Sub(self)
self.b = Sub(self)
self.sub_list.append(self.a)
self.sub_list.append(self.b)
Then you can do:
x = Main()
x.a.remove_self()
or
x.sub_list[0].remove_self()
Or alternatively, and this is the approach I recommend, get rid of remove_self
and add a new function to Main
that takes care of removing Sub
instances:
class Main:
def __init__(self):
self.sub_list = []
self.a = Sub(self)
self.b = Sub(self)
self.sub_list.append(self.a)
self.sub_list.append(self.b)
def removeSub(self, index):
self.sub_list.pop(index)
x = Main()
x.removeSub(0)
Separation of concerns works by having you split code on the bases of function, as in OOP. Whatever model you’d like to have in a different file is called into your main program using "from filename_without.py> import class_name" as is in your models file.
Please have a look at the following code:
class Main:
def __init__(self):
# list to keep track of instances of Sub()
self.sub_list = []
# create instances of Sub()
self.a = Sub()
self.b = Sub()
# append instances to sub_list
self.sub_list.append(self.a)
self.sub_list.append(self.b)
class Sub:
# function to remove the instance from sub_list
def remove_self(self):
x.sub_list.remove(self)
x = Main()
# remove instances of Sub() from sub_list
x.a.remove_self()
x.b.remove_self()
My goal is to have objects with a method to delete themselves from a list in a different class, where they are created.
The above code works for this, but I don’t like that I have to call the method by using x
directly.
The main issue I’d like so solve, however, is that I can’t figure out how to move the class Sub
to a separate file, as I can’t access sub_list
anymore.
Is there a better way to do this?
Side note: I’m working on a project with PyQt, where Main
is my MainWindow and Sub
represents some UI elements that can be dynamically created and also closed. I’d like to keep all functionality within these elements if possible.
The ideal solution would be to not have a sub_list
at all. It seems unnecessary since instances of Sub
each get a field to themselves in your example. That being said, if you want to move Sub
to a different file you can do it like so:
sub.py
:
class Sub:
def __init__(self, parent):
self.parent = parent
def remove_self(self):
self.parent.sub_list.remove(self)
And then in your main.py
:
from sub import Sub
class Main:
def __init__(self):
self.sub_list = []
self.a = Sub(self)
self.b = Sub(self)
self.sub_list.append(self.a)
self.sub_list.append(self.b)
Then you can do:
x = Main()
x.a.remove_self()
or
x.sub_list[0].remove_self()
Or alternatively, and this is the approach I recommend, get rid of remove_self
and add a new function to Main
that takes care of removing Sub
instances:
class Main:
def __init__(self):
self.sub_list = []
self.a = Sub(self)
self.b = Sub(self)
self.sub_list.append(self.a)
self.sub_list.append(self.b)
def removeSub(self, index):
self.sub_list.pop(index)
x = Main()
x.removeSub(0)
Separation of concerns works by having you split code on the bases of function, as in OOP. Whatever model you’d like to have in a different file is called into your main program using "from filename_without.py> import class_name" as is in your models file.