PDA

View Full Version : Confusion about Python's "import" statement



Volt9000
June 6th, 2009, 05:02 PM
I'm a bit confused about how Python's "import" statement works.

Let's say I have two files, as follows.

class1.py:


class Foo:
def __init__(self):
print "Class Foo initialized."


class2.py:


import class1
c = Foo()


When I execute class2.py, I get the error that "Foo" is not defined. But I imported class1, which contains the definition for Foo, so why doesn't this work?

matmatmat
June 6th, 2009, 05:05 PM
import class1
c = class1.Foo()

unutbu
June 6th, 2009, 05:10 PM
An alternative to matmatmat's solution is


from class1 import Foo
c = Foo()

StunnerAlpha
June 6th, 2009, 05:14 PM
An alternative to matmatmat's solution is


from class1 import Foo
c = Foo()
Couldn't you also do something like:


from class1 import *
c = Foo()


And that would import everything from that file, correct?

unutbu
June 6th, 2009, 05:16 PM
Yes, but I read somewhere that


from class1 import *

is not recommended because it makes it hard to track down where objects are defined.
It is recommended that you explicitly list all objects that you import into the current namespace.

Volt9000
June 6th, 2009, 06:03 PM
Thanks for your replies, everyone.

I was confused because I come from a C/C# background, and in C# when you say something like



using Some.Namespace;


that automatically imports everything defined in the namespace; no need to explicitly specify namespaces.