473,395 Members | 1,652 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

noob import question

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?

Thank you - your time is appreciated.

Brian
br***@brianandkate.com


May 19 '06 #1
11 1474

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
br***@brianandkate.com


May 19 '06 #2
> 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?


The

from <module> import <*|nameslist>

syntax imports some or all names found in <module> into the current modules
namespace. Thus you can access your class.

But if you do

import <module>

you only get <module> in your current namespace. So you need to access
anything inside <module> by prefixing the expression. In your case, it is

Student.Student

If you only write Student, that in fact is the MODULE Student, which
explains the error message.

Now while this sounds as if the from <module> import * syntax is the way to
go, you should refrain from that until you really know what you are doing
(and you currently _don't_ know), as this can introduce subtle and
difficult to debug bugs. If you don't want to write long module-names, you
can alias them:

import <moduel-with-long-name> as <shortname>
And it seems as if you have some JAVA-background, putting one class in one
file called the same as the class. Don't do that, it's a stupid restriction
in JAVA and should be avoided in PYTHON.

Diez
May 19 '06 #3
Thank you for your responses. I had a feeling is had something to do
with a namespace issue but I wasn't sure.

You are right, I do come from a Java background. If it is poor form
to name your class file the same as your class, can I ask what the
standard is?

Thanks again,
Brian

On May 19, 2006, at 8:33 AM, Diez B. Roggisch wrote:
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?


The

from <module> import <*|nameslist>

syntax imports some or all names found in <module> into the current
modules
namespace. Thus you can access your class.

But if you do

import <module>

you only get <module> in your current namespace. So you need to access
anything inside <module> by prefixing the expression. In your case,
it is

Student.Student

If you only write Student, that in fact is the MODULE Student, which
explains the error message.

Now while this sounds as if the from <module> import * syntax is
the way to
go, you should refrain from that until you really know what you are
doing
(and you currently _don't_ know), as this can introduce subtle and
difficult to debug bugs. If you don't want to write long module-
names, you
can alias them:

import <moduel-with-long-name> as <shortname>
And it seems as if you have some JAVA-background, putting one class
in one
file called the same as the class. Don't do that, it's a stupid
restriction
in JAVA and should be avoided in PYTHON.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


May 19 '06 #4
Brian Blazer wrote:
Thank you for your responses. I had a feeling is had something to do
with a namespace issue but I wasn't sure.

You are right, I do come from a Java background. If it is poor form
to name your class file the same as your class, can I ask what the
standard is?


Consider python modules what packages are in JAVA - aggregations of related
classes. Only if your module grows to an unmanagable size, split it up into
two modules.

Diez
May 19 '06 #5
PA

On May 19, 2006, at 15:33, Diez B. Roggisch wrote:
And it seems as if you have some JAVA-background, putting one class in
one
file called the same as the class. Don't do that, it's a stupid
restriction
in JAVA and should be avoided in PYTHON.


Restrictive or not, what's so fundamentally devious in putting a class
declaration in a separate file whose name is that of the declared class
(class Queue -> Queue.py)?

Sounds like a handy way of organizing your code, no?

Cheers

--
PA, Onnay Equitursay
http://alt.textdrive.com/

May 19 '06 #6
"PA" <pe************@gmail.com> wrote:
Restrictive or not, what's so fundamentally devious in putting a class
declaration in a separate file whose name is that of the declared class
(class Queue -> Queue.py)?
nothing.
Sounds like a handy way of organizing your code, no?


sure, if you prefer to do things that way.

the Python style guide (PEP 8) used to recommend naming a module that con-
tains only one class (plus support factories and other functions) after the class,
but now recommends using other names for the module, to avoid confusion.

for some reason, some people seem to treat the latest edition of each PEP as
a divine truth, and all earlier editions as works of the devil. I guess they reset
their brain before each svn update.

</F>

May 19 '06 #7
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 'o****@xiludom.gro'.split('@')])"
May 19 '06 #8
Brian Blazer wrote:
<ot>
please, dont top-post, and edit out irrelevant material
</ot>
You are right, I do come from a Java background.


Then you may want to read this:
http://dirtsimple.org/2004/12/python-is-not-java.html

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 19 '06 #9
PA wrote:

On May 19, 2006, at 15:33, Diez B. Roggisch wrote:
And it seems as if you have some JAVA-background, putting one class in
one
file called the same as the class. Don't do that, it's a stupid
restriction
in JAVA and should be avoided in PYTHON.


Restrictive or not, what's so fundamentally devious in putting a class
declaration in a separate file whose name is that of the declared class
(class Queue -> Queue.py)?

Sounds like a handy way of organizing your code, no?


Not in Python. Python classes tend to be smaller than Java classes.
Also, Python, while fully OO, is not anal-retentive about putting
everything in classes - functions are fine too. Would you advocate
putting each function in its own file ?-) And finally, with Python's
file<->module equivalence, grouping related classes and functions in a
same module greatly simplify imports.

wrt/ naming, having the module named after the class (your example)
leads to confusions like the one experimented by the OP. FWIW, Zope2
uses this convention, and I found this confusing too.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 19 '06 #10
[Please don't top-post. Please don't indiscriminately quote the entire
message you respond to.
<URL:http://en.wikipedia.org/wiki/Top_posting>]

Brian Blazer <br***@brianandkate.com> writes:
Thank you for your responses. I had a feeling is had something to
do with a namespace issue but I wasn't sure.
Another point to note is that 'from foo import *' is bad practice. It
causes a problem known as "namespace pollution", where you can't tell
where a particular name you're using comes from, or if you have
accidentally clobbered an existing name.

Either import the module to a single name, so you can qualify the
names inside that module:

import foo
import awkward_long_module_name as bar
foo.eggs()
bar.beans()

Or, if you only need a few objects from the module, import those
specifically and use their names directly:

from foo import spam, eggs
eggs()
spam()
You are right, I do come from a Java background. If it is poor form
to name your class file the same as your class, can I ask what the
standard is?


Since we don't need to have one file per class, the convention is to
group your modules by coherent functionality. Place any names,
functions, classes, whatever that all relate to a discrete, coherent
set of functionality in a single module.

The Python coding guidelines[0] also recommend that the module be
named all in lower-case, but that's more about consistency within your
own code base.

[0]: <URL:http://www.python.org/dev/peps/pep-0008>

--
\ "I have a map of the United States; it's actual size. It says |
`\ '1 mile equals 1 mile'... Last summer, I folded it." -- Steven |
_o__) Wright |
Ben Finney

May 20 '06 #11
PA wrote:
On May 19, 2006, at 15:33, Diez B. Roggisch wrote:
And it seems as if you have some JAVA-background, putting one class in
one
file called the same as the class. Don't do that, it's a stupid
restriction
in JAVA and should be avoided in PYTHON.


Restrictive or not, what's so fundamentally devious in putting a class
declaration in a separate file whose name is that of the declared class
(class Queue -> Queue.py)?

Sounds like a handy way of organizing your code, no?


Handy for a lazy programmer, maybe. Confusing for the reader, though
(do you mean Queue module, or Queue class? I must scroll up...). And
highly tacky. I recommend avoiding it. For modules, I recommend "act
of" words (words ending in -ing and -ion) because such words aren't
common identitiers. So queuing instead of Queue.

Unfortunately, the Python library isn't setting a good example here.
Too much glob.glob, time.time, socket.socket, and Queue.Queue. I hope
all these go away in Python 3000.
Carl Banks

May 20 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Williams | last post by:
First off...total noob to VB. So far have learned a lot in 1.5 days and feel fairly comfortable at throwing screens up. However, I want to start writing forms that revolve around an access...
1
by: Raaijmakers, Vincent \(GE Infrastructure\) | last post by:
Question: my src path looks like this: src\root\sub1 src\root\sub2 My main code is in root, lets say main.py and there is also a lib.py. In sub1 there if foo1.py and sub2 foo2.py Sorry for...
1
by: davestrike | last post by:
I am a noob to sql and asp programming. I am working on a db for the gaming squad I am a member of. One of the pages I created is a roster list of all the squad members. Part of this roster is...
2
by: What nickname do you want? | last post by:
I want to provide secured acces to a MySQL database. This is what I've done. Firstly the relevant pages are in a folder to which Apache requires password authentication. Then I have an HTML page...
3
by: mh | last post by:
So on most modules I import, I can access the .__file__ attribute to find the implementation. ie: >>> import time >>> time.__file__ '/data1/virtualpython/lib/python2.3/lib-dynload/timemodule.so'...
4
by: Brian Blais | last post by:
Hello, I am trying to organize some of my code, and am having a little trouble with the import logic. I find I often have something like: MyPackage/ Part1/ # wants to use functions in...
1
by: narpet | last post by:
Hello all... I have a (probably very) easy question for the SQL experts here... Lets say I have a table with the following rows and data: Name Nickname Middlename...
3
by: Richhpcnec | last post by:
I have two text strings that I have read from a text file. The text strings that I have read are the 8 characters each, 859c58d4 and 80434d43. I have assigned them the varible names of string1 and...
0
by: Gary Herron | last post by:
Dan Yamins wrote: Because loading (and reloading) assigns values to variables (called binding a value in Python), but does not go on a hunt to find variables to *unbind*. Once a variable is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.