I have two classes in python:
- ClassA gets strings from a larger string among other similar methods
- ClassB removes strings from a larger string among other similar methods
It's more complicated than that, which is the reason I'm using classes. Initially I had all methods in one class, but it was getting a little bit unreadable, and I wanted to separate the methods logically into two segments, so I moved some of the functions in to ClassB. ClassA and ClassB do not have a parent-child relationship, and are similar, but I want them to remain separated.
I want to be able to access ClassB methods from ClassA. Is there a way where I can access ClassB methods from Class A without having to create a new object; by doing something like importing or adding ClassB's methods to Class A.
class readText: def __init__(self, book): self.book = book def getPageFromNum(self): ##Some Code def getLineFromPage(self): ##Some Code class modifyText: def __init__(self, book): self.book = book def removeLine(self, lineNumber): ##Some Code def removeWordInstances(self, word) ##Some Code readableBook = readText(book) ##Accessing methods of the objects classes readableBook.getLineFromPage() ##I want to be able to access methods of the other class modifyText in the same way like below readableBook.removeLine(50) I have a second question, assuming question 1 is possible. I have around 30 methods in classA and 15 methods in classB so far. Is there way when initializing the a classA object, that I can have a optional parameter, to add the additional methods. I will always need to use ClassA methods on a string, but I don't always need classB methods on a string. Would this make the program more efficient in any significant way?