1

So apparently I just don't know how to import things correctly in a Python program. I'm just starting with the language, and it's much different from the Java that I'm used to.

Anyway, the main problem is that there's something wrong with how I'm importing packages/modules/classes and I can't seem to figure out what it is.

Right now my file structure looks like:

-Main Directory
 main.py
    -Person (Folder)
        __init__.py
        Person.py
        Student.py

Right now my main.py file looks like..

from Person import Person
from Person import Student

if __name__ == '__main__':

    p = Person.Person("Jim", 20)
    print(p)
    s = Student("Jim", 20, "math")
    print(s)

and I keep getting an error of TypeError: 'module' object is not callable

Have tried changing it to be s = Student.Student("Jim", 20, "Math") but when that happens I end up with an error of TypeError: module.__init__() takes at most 2 arguments (3 given)

For reference,

Person.py:

class Person():    
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return "My name is {0} and I am {1}".format(self.name, self.age)

Student.py:

from Person import Person
class Student(Person.Person):

    def __init__(self, name, age, sub):
        Person.__init__(self,name,age)
        self.sub = sub

No matter what I do to the imports or anything I can seem to change, it all keeps giving me errors. No idea what to do at this point - maybe I just missed the creating of classes and subclasses when it was shown to me, but I can't figure out anything to fix it.

Ben
  • 31
  • 1
  • 1
  • 3
  • see http://stackoverflow.com/questions/2360724/in-python-what-exactly-does-import-import – mjv Jul 20 '11 at 03:16
  • 3
    You would probably have an easier time of this if you use lowercase in your modules and uppercase in your class names. Check out naming conventions: http://www.python.org/dev/peps/pep-0008/ – Mark Hildreth Jul 20 '11 at 03:21
  • 1
    also; Python is not Java. There's no reason whatsoever to put each class in its own module; `person.Person` and `person.Student` both defined in `person/__init__.py` or `person.py` is perfectly reasonable. – Wooble Jul 20 '11 at 03:39
  • Similar: [TypeError: module.__init__() takes at most 2 arguments (3 given)](http://stackoverflow.com/q/14583761/55075) – kenorb May 17 '15 at 20:30

2 Answers2

2

main.py:

from Person import Person
from Person import Student

if __name__ == '__main__':

    p = Person.Person("Jim", 20)
    print(p)
    s = Student.Student("Jim", 20, "math")
    print(s)

student.py

from Person import Person

class Student(Person):

    def __init__(self, name, age, sub):
        super(Student, self).__init__(name,age)
        self.sub = sub

person.py

class Person(object):    
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return "My name is {0} and I am {1}".format(self.name, self.age)
agf
  • 148,965
  • 36
  • 267
  • 227
  • Changed everything as suggested, but still having the same TypeError. Maybe I just created the subclasses incorrectly? – Ben Jul 20 '11 at 03:22
  • OK, reedited to correct, PythonWin wasn't actually reloading changed modules. `class Person` should inherit from object. `class Student` should inherit from Person, and call it's constructor with `super(Student, self).__init__(name,age)`. Note that in Student.py you're importing a class, whereas in main.py you're importing a module. – agf Jul 20 '11 at 03:29
  • Finally got it to work changing it to `class Student(Person.Peron):` or changing the import to `from Person.Person import Person` PyDev still tells me something there is wrong, but it works. – Ben Jul 20 '11 at 03:43
  • You might want to close and reopen PyDev and recheck, I know in PythonWin the globals get polluted. The versions above run for me in a freshly opened interpreter. – agf Jul 20 '11 at 03:44
0

The problem is in your Student class. Here, Person refers to the Person.py module. You should call the parent object by doing:

super().__init__(name,age)

Also, in the main part, you should initialize:

s = Student.Student("Jim", 20, "math")
Charles Brunet
  • 18,389
  • 21
  • 77
  • 120