Importing Definitions

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Sep 5 13:28:13 EDT 2013


On Thu, 05 Sep 2013 22:50:35 +1000, Chris Angelico wrote:

> On Thu, Sep 5, 2013 at 10:39 PM, Azureaus <lo0446 at my.bristol.ac.uk>
> wrote:

>> Now I know if there was a method I wanted to reference I could do
>> something like this in module2. from module1 import method1
>>
>> which would mean from that point on I could just reference it as
>> method1 rather than module1.method1, is there such a way I could do
>> this with definitions??
> 
> You can! Any name will work, functions aren't special.
> 
> from module1 import method1, A, B, C, D, E

Better practice is to use:

import module1
print module1.A
print module2.B

and so forth since that makes it far more clear what you are doing and 
where they come from. But it's not compulsory.



> If you want all of them at once, you may want:
> 
> from module1 import *
> 
> but this can make your code confusing.

Shame on you Chris :-) Don't teach newbies to set their head on fire 
before teaching them where the fire extinguisher is.

Azureaus, don't use the form "from module import *" unless you really 
need to, and as a beginner, you almost never really need to.

(And as an expert, you will really need to even less!)


-- 
Steven



More information about the Python-list mailing list