0

I have two classes in python:

  1. ClassA gets strings from a larger string among other similar methods
  2. 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?

1
  • You have to either use the object of classB in classA, or use parent child, if the methods are instance methods, and not class or static methods Commented Dec 29, 2019 at 20:12

2 Answers 2

2

Probably the cleanest and least-hacky model to use relies on the mix-in inheritance pattern, possibly combined with Abstract Base Classes.

The basic principle is that you define a mix-in class that is not intended to be instantiated on its own, but is used to implement the methods that get shared by its related classes.

So you can have something like this:

class MixinSource: ... def shared_method1(self, param1): ... def shared_method2(self): ... def overwrite_me(self, param1, param2): ... class Foo(SomeOtherClass, MixinSource): ... class Bar(YetAnotherClass, MixinSource): ... def overwrite_me(self, param1, param2): ... 

What this code basically does is it allows you to keep the shared code in the MixinSource class, overwriting those shared methods if and when necessary.

In the example above, if you have an instance of either Foo or Bar, they will both have the same methods as in MixinSource as well as any additional methods you create in those inheritor classes. Furthermore, the Bar class will have a different implementation of the overwrite_me method from MixinSource.

It also allows you to inherit from other classes so you can keep your domains distinct, as that sounds like it is your preference. You can read more about Python’s mix-in patterns here: https://softwareengineering.stackexchange.com/questions/312339/are-python-mixins-an-anti-pattern

There are also more advanced ways of modeling this concept involving Abstract Base Classes. Basically, you define an ABC that is your mix-in, you define the shared methods that must be implemented by the inheriting classes (using the abstractmethod decorator), and then you define the shared methods that get implemented within the ABC itself. If you really want, you can get super fancy with the ABC registration and subclass hooking logic (see Python docs linked above) to apply conditional logic / conditional registration of the ABC to your inheriting classes based on their properties. However, this would be a major anti-pattern, and would make your code quite difficult to follow for anyone maintaining your code (imagine trying to follow that logic yourself three or four years down the road).

So you can do it if you want to, but I would definitely consider carefully whether that added complexity is really worth it.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

1

Learn about classes and inheritance and you might end up with something like

class ModifiableText(object): ... class ModifiableBook(ModifiableText): ... 

If this is something you'd like to do in life, I suggest googling "domain modeling" as well and reading up.

1 Comment

I understand inheritance already. Although I could achieve what I want with having the modifiableBook class inheriting class methods and states from the other class, I would rather not, because they don't have a parent-child relationship. I want my code to reflect that they do not have this relationship, so I want to extend ClassA's methods by importing classB's methods into classA if a parameter is given when initialializing a classA object

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.