Classes and Functions - General Questions
John Salerno
johnjsal at NOSPAMgmail.com
Thu Oct 19 10:26:10 EDT 2006
John Salerno wrote:
> Setash wrote:
>
>> And have class2 inherit class1 without any import statements, or need
>> it be imported first?
>> Or need class1 and class2 be both declared in the same .py file if
>> there is inheritance?
>
> If the classes are in the same module, you don't need to do any
> importing or qualification. If they are in separate modules, you need to
> import the necessary module(s) and then you can use its contents.
Quick clarification: even if you import a module, you still need to
qualify a call to its attributes:
----
import sys
print sys.version #not 'print version'
----
But you can use the 'from <module> import <attribute>' format to avoid this:
----
from sys import version
print version
----
But this is very bad to do. The recommendation that I like is to only
use from/import when you want a module from a package, not when you want
classes, methods, etc. from a module.
More information about the Python-list
mailing list