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

MRAB python at mrabarnett.plus.com
Mon Nov 14 22:31:21 EST 2016


On 2016-11-15 01:36, jfong at ms4.hinet.net wrote:
> Ned Batchelder at 2016/11/15 6:33:54AM wrote:
>> > But I get a wrong answer this way:
>> > >>> from deen import *
>> > >>> tblm = tbli
>> > >>> gpa(0x7d)
>> > 125  # it's 0x7d, the tblm[2] is 0
>> >
>> > Why? why! why:-(
>>
>> Here you are assigning a value to your own tblm, not deen.tblm,
>> so gpa does not see the new value.
>>
> Hi Ned, below is the capture of my terminal screen:
>
> D:\Temp>python
> Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (In
> tel)] on win32
> Type "help", "copyright", "credits" or "license" for more information.
>>>> dir()
> ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
>
>>>> from deen import *
>>>> dir()
> ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__',
>  'gpa', 'tbli', 'tblm']
>>>> tblm
> [0, 0, 0]
>>>> tblm=tbli
>>>> tblm
> [102, 39, 208]
>>>> dir()
> ['__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__',
>  'gpa', 'tbli', 'tblm']
>>>>
>
> Do you mean the last 'tblm' is my own? How it can be? The 'tblm' already exist before the assignment and this assignment shouldn't create a new one, right? and how do I assign the 'deen.tblm' under this circumstance?
>
> --Jach
>
In the module 'deen' there's the name 'tblm' and the name 'tbli', among 
others:

     [module deen]

     tblm -----------> [0, 0, 0]

     tbli -----------> [102, 39, 208]

When you say "from deen import *" you're copying names and their 
references from the module's namespace to your local namespace:

     [module deen]                                   [locally]

     tblm ------------> [0, 0, 0] <----------------- tblm

     tbli ------------> [102, 39, 208] <------------ tbli

When you say "tblm = ..." you're binding the local name to a different 
object, so when you say "tblm = tbli" you get:

     [module deen]                                   [locally]

     tblm ------------> [0, 0, 0]    :-------------- tblm
                                     V
     tbli ------------> [102, 39, 208] <------------ tbli




More information about the Python-list mailing list