help on "from deen import *" vs. "import deen"

Steven D'Aprano steve+comp.lang.python at pearwood.info
Thu Nov 17 03:03:50 EST 2016


On Thursday 17 November 2016 17:48, jfong at ms4.hinet.net wrote:

> Steven D'Aprano at 2016/11/17 12:06:19PM wrote:
>> You understand how this works?
> 
> Yes, thank you for your detail explanation.
> 
>>     import russia as _tmp
>>     president = _tmp.president
>>     del _tmp
> 
> This one I can understand. But the previous one
> 
>>>_tmp = int('5')
>>>for name in dir(_tmp):
>>>    if not name.startswith('_'):
>>>        locals()[name] = getattr(_tmp, name)
>>>del _tmp
> 
> which I am still on scratching my head.


dir(obj) returns a list of all the attributes of obj:

py> dir(5)
['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', 
'__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', 
'__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', 
'__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', 
'__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', 
'__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', 
'__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', 
'__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', 
'__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', 
'__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 
'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']


Most of those are __ dunder __ methods reserved by Python. Don't touch them.

So if you look at each name in turn, pick out the ones that DON'T begin with an 
underscore:

['bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']


Then use getattr(obj, name) to return the value of that attribute, which may be 
a method:

py> getattr(5, 'bit_length')
<built-in method bit_length of int object at 0x86ae2b0>


and then save it in the local namespace:

py> x = -1
py> locals()['x'] = 999
py> x
999


(But watch out: assignment using locals() doesn't always work, for technical 
reasons it may not work inside a function.)


> Now the question moves from "how" to "why":
> 
> Why "del _tmp" at the last step? 

Technically, there is no "del _tmp" because there is no _tmp created first. The 
import command doesn't create the _tmp variable in the first place, so there's 
no need to delete it.

But there's no easy way to do that from pure Python code, so the easiest way is 
to use a temporary variable and then delete it afterwards.


> The only reason I can thought of is
> "information hiding", but from whom? A global variable has its reason to be
> as a global. It may need to be modified later to influence others behavior.
> Using delete to hide the name seems unnecessary and redundant. 

This has very little to do with global variables. This is about importing 
names. If you want to import the module as a whole, then use

    import module

but if you want to import attributes of the module, then use:

    from module import foo, bar, baz

If you want both, do both.



The most important thing you should learn from this thread is:

- avoid using "from module import *" as it is usually more trouble 
  than it is worth.


It is confusing and leads to more problems than it solves. If Python was being 
invented now, rather than 20 years ago, I would expect that there would be no 
"import *" in the language.




-- 
Steven
299792.458 km/s — not just a good idea, it’s the law!




More information about the Python-list mailing list