Inheriting a class of the same name in python

I am new to python and I am trying to create two classes with the same name in two different source files to call them "Main.py" and "Extension.py" and the class is "MyClass". MyClass in Extesntion.py, derived from MyClass in Main.py. If it works, then when I create myclass object, I import the extension into my code, then I will have more functionality compared to Main.py.

Main.py

    class MyClass:
       def __init__(self):
         Initialize something

       def foo1(self, a, b):
         Do something

      

Then the extension will look like this: Extensions.py

    import Main

    class MyClass(MyClass):
       def __init__(self):
         Initialize something

       def foo2(self, a, b):
         Do something

       def foo3(self, a, b):
         Do something

      

and then if i have code like this i expect i can't use foo2 and foo3

    import Main

    myclass = MyClass()
    myclass.foo1(a, b)

      

and finally, if I have code like this, I expect that I will use all the functions

    import Extension

    myclass = MyClass()
    myclass.foo1(a, b)
    myclass.foo2(a, b)
    myclass.foo3(a, b) 

      

Any help would be appreciated :)

+3


source to share


4 answers


If you do

import main

      

you need to use main.MyClass

to create an object from main.py.

Instead, you can do

from main import MyClass

      



so that it is directly accessible.

If you want two different classes with the same name, you can instead do

from main import MyClass as MainClass

      

and you will get a class called MainClass

+3


source


If you don't from Extension import *

, you will need to specify the module to access the class.

import Main
import Extension
foo = Main.MyClass()
bar = Extension.MyClass()

      



If you don't want to specify a module, then the only way to avoid name clashes is to import the class with a different name, for example:

from Main import MyClass as ClassA

+1


source


It's pretty straightforward when you explicitly import the given name using syntax from {module} import {name}

.

main.py

class MyClass:
    def __init__(self):
        pass

    def foo1(self, a, b):
        pass

      

extensions.py

from main import MyClass

class MyClass(MyClass):
    def __init__(self):
        pass

    def foo2(self, a, b):
        pass

    def foo3(self, a, b):
        pass

      

client_main.py

from main import MyClass

myinstance = MyClass()
myinstance.foo1(a, b)

      

client_extensions.py

from extensions import MyClass

myinstance = MyClass()
myinstance.foo1(a, b)
myinstance.foo2(a, b)
myinstance.foo3(a, b)

      

+1


source


Typically, in this case, you must follow through import as

. This allows you to use your import's alias as the new name. So, in the file where your second class is, import the first class as:

from main import MyClass as MainMyClass

Then, while doing your inheritance, refer to MainMyClass

:

class MyClass(MainMyClass):

0


source







All Articles