Automatic import ?

Aahz aahz at pythoncraft.com
Sat Apr 10 19:48:30 CEST 2010


In article <4bac361d$0$8840$c3e8da3 at news.astraweb.com>,
Steven D'Aprano  <steve at REMOVE-THIS-cybersource.com.au> wrote:
>On Thu, 25 Mar 2010 18:03:58 -0700, C. B. wrote:
>> 
>> from mymodule import AAA
>> from mymodule import BBB
>> 
>> a = AAA(BBB()))
>> 
>> But, as there is no case where AAA can be used without BBB, I would like
>> to avoid importing BBB in my Python scripts when I already import AAA.
>
>Since AAA must take an argument of BBB, then give it a default:
>
># in mymodule
>def AAA(arg=BBB()):
>    ...

That would frequently give wrong results unless BBB is explicitly
designed to create immutable instances.  I strongly suggest doing the
usual mutable dance:

def AAA(arg=None):
    if arg is None:
        arg = BBB()
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"It is easier to optimize correct code than to correct optimized code."
--Bill Harlan



More information about the Python-list mailing list