Zach Gates
Zach Gates

Reputation: 4155

What is causing this TypeError?

I have two different files, containing two different classes (Let's call them Foo and Bar).

In the file (Foo.py) with class Foo, I have:

class Foo:
    def __init__(self):

    ...

And in the file (Bar.py) with class Bar, I have:

from Foo import Foo

class Bar(Foo.Foo):
    def __init__(self):

    ...

When I run my code, I get this TypeError:

Traceback (most recent call last):
  File "Bar.py", line 2, in <module>
    class Bar(Foo.Foo):
TypeError: Error when calling the metaclass bases
    __init__() takes exactly 1 argument (4 given)

Why is it telling me I have 4 arguments to pass __init__ when the only argument I have in the code is self?

Upvotes: 0

Views: 63

Answers (1)

steveha
steveha

Reputation: 76755

It looks to me like your problem is that you are importing the class directly, but then trying to access it via the module name.

If you have a class Foo inside a module source file foo.py then you can use it like this:

import foo
new_instance = foo.Foo()

You can also do this:

from foo import Foo
new_instance = Foo()

But you are trying to do this:

from Foo import Foo
new_instance = Foo.Foo()

In the expression Foo.Foo(), the first Foo is your class. Then after the . Python parses out Foo and looks for a class member called Foo.

Note: I suggest you comply with PEP 8 guidelines, and your modules should use lower-case names. Thus foo.py rather than Foo.py.

http://legacy.python.org/dev/peps/pep-0008/

Upvotes: 2

Related Questions