1

society. I'm trying to understand the OOP programming and I'm facing some issues and asking for help.

Here the example:

I'm trying to create all objects under one class and then I want to inherit from this class.

test_class/baseclass.py

from test_class.first_class import FirstClass
from test_class.second_class import SecondClass


class MainClass:
    def __init__(self):
        self.firstclass = FirstClass()
        self.secondclass = SecondClass()

test_class/first_class.py

from test_class.baseclass import MainClass

    class FirstClass(MainClass):
        def __init__(self):
            MainClass.__init__(self)

        def add_two_number(self):
            return 2 + 2

test_class/second_class.py

from test_class.baseclass import MainClass

class SecondClass(MainClass):
    def __init__(self):
        MainClass.__init__(self)

    def minus_number(self):
        return self.firstclass.add_two_number() - 10


if __name__ == '__main__':
    print(SecondClass().minus_number())

When I run the last file I get this error

Traceback (most recent call last):
  File "/Users/nik-edcast/git/ui-automation/test_class/second_class.py", line 1, in <module>
    from test_class.baseclass import MainClass
  File "/Users/nik-edcast/git/ui-automation/test_class/baseclass.py", line 1, in <module>
    from test_class.first_class import FirstClass
  File "/Users/nik-edcast/git/ui-automation/test_class/first_class.py", line 1, in <module>
    from test_class.baseclass import MainClass
ImportError: cannot import name MainClass

it's just an example, but I have different code. and I'm looking for a solution based on this example

  • 2
    You have circular dependencies, `a` cant import `b` if `b` imports `a`. Also in python, there is no reason to split up your code into so many different files, unlike java you can multiple classes per file. – ktzr May 13 '18 at 03:23
  • What's the issue dude? – MadeOfAir May 13 '18 at 03:26
  • @MadeOfAir I'm getting ImportError. I updated post. – Nikolas Miller May 13 '18 at 03:28
  • What's the use case of having inheritance in your code? I think you can simply remove the inheritance part and everything will work smoothly (at least in this simple snippet). Just write `class FirstClass():`, `class SecondClass():` and remove the `from test_class.baseclass import MainClass` line. – slackmart May 13 '18 at 03:33
  • sorry I forgot to mention. it's just example, but I have different code. and I'm looking for solution based on this example – Nikolas Miller May 13 '18 at 03:36
  • Perhaps you could try to explain the use case. Don't post your code but the inheritance use case. BTW @ktzr has the answer for you (in the absence of a valid use case). – slackmart May 13 '18 at 03:38
  • I posted the real case what I have in my code. But basically I created objects in MainClass and I tried to inherit from MainClass, all classes. In a post, I have a similar example of my actual code. – Nikolas Miller May 13 '18 at 03:43

1 Answers1

0

I changed your code to this:

baseclass.py

import first
import second

class MainClass:
    def __init__(self):
        self.firstclass = first.FirstClass()
        self.secondclass = second.SecondClass()

first.py

import baseclass

class FirstClass(baseclass.MainClass):
    def __init__(self):
        baseclass.MainClass.__init__(self)

    def add_two_number(self):
        return 2 + 2

second.py

import baseclass

class SecondClass():
    def __init__(self):
        baseclass.MainClass.__init__(self)

    def minus_number(self):
        return self.firstclass.add_two_number() - 10

if __name__ == '__main__':
    print(second.SecondClass().minus_number())

I get no ImportErrors or any errors of any kind. I think your ImportError had something to do with from instead of just import. I hope this helps.

  • 1
    thanks, for your help. But your example doesn't work for me. `Traceback (most recent call last): File "/Users/nik-edcast/git/ui-automation/test_class/second_class.py", line 1, in import baseclass File "/Users/nik-edcast/git/ui-automation/test_class/baseclass.py", line 1, in import first_class File "/Users/nik-edcast/git/ui-automation/test_class/first_class.py", line 4, in class FirstClass(baseclass.TestMainClass): AttributeError: 'module' object has no attribute 'TestMainClass' ` – Nikolas Miller May 13 '18 at 15:22
  • Are all of your files in the same folder? – Aiden Blishen Cuneo May 13 '18 at 21:37
  • It looks like the file first_class.py is trying to access TestMainClass from baseclass.py. Does that class exist in baseclass.py? – Aiden Blishen Cuneo May 14 '18 at 05:02