Mohsen67
Mohsen67

Reputation: 135

Inheriting a class with the same name in Python

I am new in Python and I am trying to create two classes with the same name in two different source files. Let’s call them "Main.py" and "Extension.py". The class is "MyClass". MyClass in Extesntion.py is derived from MyClass in file Main.py. If it works then when I create an object myclass and I import Extension in my code, then I would have more functions in comparison with file Main.py.

File Main.py

    class MyClass:
       def __init__(self):
         Initialize something

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

Then extension would be like this:

File 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 that 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 use all the functions.

    import Extension

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

Upvotes: 10

Views: 7817

Answers (4)

Łukasz Rogalski
Łukasz Rogalski

Reputation: 23223

It's quite easy when you explicitly import the given name using the from {module} import {name} syntax.

File main.py

class MyClass:
    def __init__(self):
        pass

    def foo1(self, a, b):
        pass

File 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

File client_main.py

from main import MyClass

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

File client_extensions.py

from extensions import MyClass

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

Upvotes: 2

lampwins
lampwins

Reputation: 920

Generally in this case, you would do an import as. This allows you to alias your import as a new name. So in the file where your second class is, import the first class as:

from main import MyClass as MainMyClass

Then when doing your inheritance, refer to MainMyClass:

class MyClass(MainMyClass):

Upvotes: 0

steliosbl
steliosbl

Reputation: 8921

Unless you do from Extension import *, you'll need to specify the module in order to access the class.

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

If you don't want to have to specify the module, then the only way to avoid a name collision is to import the class under a different name like so:

from Main import MyClass as ClassA

Upvotes: 1

L3viathan
L3viathan

Reputation: 27283

If you do

import main

you'll need to use main.MyClass to create an object from main.py.

Instead you can do

from main import MyClass

to have it available directly.

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

from main import MyClass as MainClass

and you'll have the class available under the name MainClass

Upvotes: 8

Related Questions