Python Class use

Magnus Lycka lycka at carmen.se
Tue Feb 7 18:01:46 EST 2006


Roel Schroeven wrote:
> import MyModule
> 
> x = MyModule.MyClass()
> x.f()
> 
> Or you could directly import MyClass into the global namespace like this:
> 
> from MyModule import MyClass
> 
> x = MyClass()
> x.f()
> 
> But that's not recommended since it clutters the global namespace and 
> makes it more difficult to see which name comes from which module.

No, no, it's completely kosher to use "from module import Class".
It won't clutter the namespace any more than the above as long
as you just import one class, and there's no "magically" appearing
names in any namespace. You should avoid "from module import *" in
your programs though, but feel free to use it in the interpreter.

Just using "from MyModule import *" and then simply using "MyClass"
in the code is confusing for others who read your code, since it's
not appearent where MyClass came from. As your progam grows and you
use more modules, it will get worse. Imagine that you have three
modules and do:

from module1 import *
from module2 import *
from module3 import *

How on earth will someone reading that progam know where to find the
definition of a class or function then, and what will happen if you
happened to use the same class or function name in more than one
module? As software grows, it gets more and more important to prevent
such problems.

With either "from module1 import Class1, Class2" or "import module1"
follwed by "o = module1.Class1()" etc, it will always be clear from
the source code file where Class1 is used, where it's defined. The
"from ... import ..." style means that you need to find the import
statement to figure out where it's from, so the plain "import x"
might be preferable, but that's a judgement call you need to make
from case to case.

But more importantly, I think "S" should realize that while the
interpreter is nice as a calculator etc and for experiments, the
normal way to use Python is to put practically all your code in
files. It's common to write code so that it's both useful as a
module and a stand alone program. You achieve by looking at the
"magic" __name__ variable, which is set to "__main__" if a script
is used as direct input to python, and to the file name (sans .py)
if imported.

E.g. if you have a file "whoami.py" containing just "print __name__",
doing "python whoami.py" will print out "__main__", but doing an
"import whoami" from the interpreter would print out "whoami".

So, if you want some code to check how many files and/or directories
you have in your current directory, you could make a module like this:

filecount.py---------------

import os

def filecount():
     return len(os.listdir('.'))

if __name__=='__main__':
     print "There are", filecount(), "entries in the current directory"

---------------------------

Then, "python filecount.py" might display

There are 12 entries in the current directory

while an interactive session might give:

 >>> import filecount
 >>> print filecount.filecount()
12
 >>>



More information about the Python-list mailing list