0

If I writing a class inside a class, and them both use same methods i.e.:

class Master:  
  def calculate(self):  
    variable = 5
    return variable
  class Child:
    def calculate(self):
      variable = 5
      return variable

Do I have to declare this method in both classes, or can I only declare it in the Master, and then use it in Child?

5
  • 1
    Why are you nesting your classes? The classes have no relationship other than that Child becomes an attribute on Master; methods are not shared. Commented Mar 22, 2014 at 18:46
  • @MartijnPieters Those classes have completely different fields, but use one or two same methods. Maybe it's not quite a good code, but an answer will be accepted greatfully. Commented Mar 22, 2014 at 18:50
  • Then why not use inheritance? Put methods that are to be shared on a base class instead. Commented Mar 22, 2014 at 18:51
  • I really want the Child to be a field of Master. Is there better method to do it? I using it in django, I want to use this relationship later to see the Child instances inside of Master fields in html. Commented Mar 22, 2014 at 19:14
  • You can always give Master a child attribute and assign a Child instance to that. Then each Master instance has a Child instance. Commented Mar 22, 2014 at 19:15

1 Answer 1

4

Nesting one class inside another has no other effect than that the nested class becomes an attribute on the outer class. They have no other relationship.

In other words, Child is a class that can be addressed as Master.Child instead of just plain Child. Instances of Master can address self.Child, which is a reference to the same class. And that's where the relationship ends.

If you wanted to share methods between two classes, use inheritance:

class SharedMethods:
    def calculate(self):  
        variable = 5
        return variable

class Master(SharedMethods):
    pass

class Child(SharedMethods):
    pass

Here both Master and Child now have a calculate method.

Since Python supports multiple inheritance, creating a mixin class like this to share methods is relatively painless and doesn't preclude using other classes to denote is-a relationships.

Leave containment relationships to instances; give your Master a child attribute and set that to a Child instance.

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

2 Comments

Thank you. If my code file will contain about 20 classes, where some of them will have same methods with 1 another class. Will it make the code unreadable, as there will be about 5 classes of shared methods for different classes? (not in my case, but interesting...)
@Alexander: If you have that many different classes with some shared methods, you need to perhaps rethink your architecture. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.