noob import question
Iain King
iainking at gmail.com
Fri May 19 09:32:59 EDT 2006
Brian Blazer wrote:
> OK, I have a very simple class here:
>
> class Student:
> """Defines the student class"""
>
> def __init__(self, lName, fName, mi):
> self.lName = lName
> self.fName = fName
> self.mi = mi
>
> Then I have a small script that I am using as a test:
>
> from Student import *
>
> s1 = Student("Brian", "Smith", "N")
>
> print s1.lName
>
> This works as expected. However, if I change the import statement to:
> import Student
>
> I get an error:
> TypeError: 'module' object is not callable
>
> I have tried to look up what is going on, but I have not found
> anything. Would it be possible for someone to take a minute and give
> an explanation?
>
I take it you are getting the error on the line
s1 = Student("Brian", "Smith", "N")
This is because when you use 'import Student', it loads the file
Student.py into a namespace called Student (unlike the 'from'
statement, which loads it into the main namespace). to access anything
from your Student module, prepend with Student. , so your line becomes:
s1 = Student.Student("Brian", "Smith", "N")
Iain
> Thank you - your time is appreciated.
>
> Brian
> brian at brianandkate.com
More information about the Python-list
mailing list