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

Michael Torrie torriem at gmail.com
Thu Nov 17 10:38:08 EST 2016


On 11/16/2016 07:01 PM, jfong at ms4.hinet.net wrote:
> Michael Torrie at 2016/11/16 11:15:11AM wrote:
>> ... The globals object is a dictionary and is itself mutable.  But
>> when we assign a new object to a particular dictionary key, it
>> tosses out the old reference and makes the key now refer to the new
>> object.  It does not do anything to the old object itself.
> 
> The last question: Is it possible, in the current Python version, to
> re-bind a global name in module "deen" after it was imported "from
> deen import *"?

Sigh. If you understood what everyone has been explaining, you'd know
that the answer to that is no.  That's not the way variables in Python
work.  The only way to rebind deen.foo is to do as Chris suggested in
his reply and always refer to it with "deen.foo".

Once a variable is imported from a library it becomes a local variable
in your module (well a local global variable, since variables can only
be global to the module itself).  It starts out life referring to the
same object as the global attribute inside of deen. But once you assign
to the local name, it is rebound to a new object.  Do you understand that?


> I mean it might be better to have two labels to label chair and couch
> separately, instead of one:-)

Like I said, whether the names you use are appropriate is completely up
to you.  But this statement seems to imply you're still not getting it
and still thinking of variables as boxes like they are in other
languages, rather than labels that can only point to one thing at a
time.  Python's variables are different from other languages, but in an
understandable way.  Sit back and think about it, play with the python
command prompt. Run id() on a variable, assign something else to it, and
run id() on it again.  As Dennis said, understanding Python variables,
though not difficult, needs to be second nature to you or you'll be
fighting Python and hating the experience.



More information about the Python-list mailing list