Suvasish Sarker
Suvasish Sarker

Reputation: 435

Facing issues in python Inheritance

Trying to test inheritance with python. My scenario is like below..

In a folder I’ve a base class named Asset_Base and a child class named Asset and they looks like below..

in Asset_Base.py file:

class Asset_Base(object):
    def __init__(self):
        pass

    def sayHello(self):
        print('Hello!')

in Asset.py file:

import Asset_Base
class Asset(Asset_Base):
    def __init__(self):
        pass

    def sayHello(self):
        super().sayHello()

a = Asset()
a.sayHello()

while i run this Asset class getting this error..

class Asset(Asset_Base):
TypeError: module.__init__() takes at most 2 arguments (3 given)

After trying few things found it works fine if i just change the import statement like below

from Asset_Base import *

I’m new to python and not sure about the difference between

import Asset_Base and from Asset_Base import *

Can anyone please explain it a bit.

Thanks in advance.

Upvotes: 2

Views: 158

Answers (2)

Rik Verbeek
Rik Verbeek

Reputation: 502

import Asset_Base

imports a module (a file)

from Asset_Base import * 

You import everything that is in your file (in this case the class Asset_Base)

For more information take a look into the Python documentation.

Upvotes: 3

Max Noel
Max Noel

Reputation: 8910

That's because in your first example, your class is inheriting from the Asset_Base module (that is, the .py file) -- not the class of the same name that it contains.

Note how your error message talks about module.__init__().

Change your import statement so that it reads from Asset_Base import Asset_Base.

In addition, "module contains a class of the same name" is an anti-pattern in Python. Avoid doing that.

Upvotes: 5

Related Questions