don't use 'from' (was Re: module variable scope)

Ian Bicking ianb at colorstudy.com
Tue Oct 28 12:07:14 EST 2003


On Tuesday, October 28, 2003, at 07:48 AM, Alex Martelli wrote:
>> I expected variable v to be changed by calling setv() but it is not.
>> Who can explain this?
>
> Who can explain your expectations, I dunno.  I hope the
> explanation of the behavior was clear.
>
> My often-repeated recommendation is: forget the from statement's
> very existence.  Use
>
> import b
>
> and refer to b.v -- NOT to bare v -- and THEN everything will work
> according to expectations.
 >
> The 'from' statement is occasionally a handy shortcut if you
> know what you're doing, but it doesn't work well when you're
> not sure what you're doing, nor when any name rebinding or
> module reloading may be involved.  That's three strikes
> against it, which means _it's out_:-).

a) Don't rebind global module variables after module initialization.  
That's a bad idea in its own right.

b) And if you don't know what you're doing then anything can go wrong.  
If you don't know what you're doing, at least don't do (a).

c) Maybe reloading is a good reason, but reloading sucks all around and 
"from" is only one of the possible problems you'll encounter.

--
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org






More information about the Python-list mailing list