noob import question

bruno at modulix onurb at xiludom.gro
Fri May 19 10:10:32 EDT 2006


Brian Blazer wrote:
> OK, I have a very simple class here:
> 
> class Student:

class Student(object):

>     """Defines the student class"""
> 
>     def __init__(self, lName, fName, mi):
>         self.lName = lName
>         self.fName = fName
>         self.mi = mi

Do yourself a favour: use meaningful names.

> Then I have a small script that I am using as a test:
> 
> from Student import *

So your module is named Student.py ? The common convention is to use
all_lower for modules, and CapNames for classes. BTW, unlike Java, the
common use is to group closely related classes and functions in a same
module.

> 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

Of course. And that's one for the reason for naming modules all_lower
and classes CapNames.

With
 from Student import *

you import all the names (well... not all, read the doc about this)
defined in the module Student directly in the current namespace. So,
since the module Student contains the class Student, in this current
namespace, the name Student refers to the class Student.

With
  import Student

you import the module name Student in the current namespace. You can
then refer to names defined in module Student with the qualified name
module.name. So here, to refer to the class Student, you need to use the
qualified name Student.Student.

You wouldn't have such a confusion if your module was named students !-)


> I have tried to look up what is going on, but I have not found 
> anything.  

you could have done something like this:

import Student
print dir()
print dir(Student)
print type(Student)
del Student
from Student import *
print dir()
print dir(Student)
print type(Student)


Also, reading the doc migh help:
http://www.python.org/doc/2.4.2/tut/node8.html

HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list