[Tutor] idle - namespaces (was idle)
alan.gauld@bt.com
alan.gauld@bt.com
Mon, 1 Apr 2002 00:16:39 +0100
------_=_NextPart_001_01C1D90A.1CCDBC30
Content-type: text/plain; charset="iso-8859-1"
I may have missed the answer to this but here goes anyway...
> away code. However I just noticed something, when I delete all the code
> to write some more throw away code the variables and I think also other
> things do not get deleted. How do I delete them?
With great difficulty.
When you import names from a module they get stored
in a dictionary. When you reload a module it simply
adds the names - which is why reload doesn't work
for the "from f import x" form.
The only reliable way to do this is to exit and restart
(Cameron another reason I use the OS command line...)
Of course avoiding the from f inmoprt x style helps a lot.
>>> dir()
['__builtins__', '__doc__', '__name__']
dir() returns a list but its built up from the underlying
dictionary. deleting from the list doesn't affect the
dictionary underneath.
>>> del a
This deletes the name from the underlying dictionary...
>>> dir()
['__builtins__', '__doc__', '__name__', 'foo']
so its gone from dir() too...
But if you try to delete several items from a list, this doesn't work,
because of the difference between the string 'a' and the name a, I think:
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'foo']
>>> dir()[3:]
['a', 'foo']
>>> for name in dir()[3:]:
del name
This deletes the string that was in the list (I think,
I admit I'm getting more shaky here...)
>>> for name in dir()[3:]:
del eval(name)
This uses the string to eval which returns the alue of
the variable named by the string - ie 3 in this case.
> So how do I get acces to the Variable a via dir() ?
I don't think you can. You have to go to the Builtins
dictionary and grub about in the innards - not recommended!
Alan G.
------_=_NextPart_001_01C1D90A.1CCDBC30
Content-type: text/html; charset="iso-8859-1"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4807.2300" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>I may have missed the answer to this but here goes
anyway...</FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002></SPAN><SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff size=2> </FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>> </FONT> </SPAN>away code. However I just noticed something,
when I delete all the code <SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff size=2> </FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>> </FONT> </SPAN>to write some more throw away code the variables
and I think also other <SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff size=2> </FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>> </FONT> </SPAN>things do not get deleted. How do I delete
them? <SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2> </FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002></SPAN> </DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>With great difficulty.</FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002></SPAN> </DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>When you import names from a module they get stored </FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>in a dictionary. When you reload a module it simply </FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>adds the names - which is why reload doesn't work </FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>for the "from f import x" form.</FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>The only reliable way to do this is to exit and
restart</FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>(Cameron another reason I use the OS command line...)</FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2></FONT></SPAN> </DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2>Of course avoiding the from f inmoprt x style helps a
lot.</FONT></SPAN></DIV>
<DIV><SPAN class=050230623-31032002><FONT face="Courier New" color=#0000ff
size=2></FONT></SPAN> </DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV><FONT face="Courier New" size=2>>>> dir()<BR>['__builtins__',
'__doc__', '__name__']<BR><SPAN class=050230623-31032002><FONT
color=#0000ff> </FONT></SPAN></FONT></DIV></BLOCKQUOTE>
<DIV dir=ltr><FONT face="Courier New" size=2><SPAN
class=050230623-31032002>dir() returns a list but its built up from the
underlying </SPAN></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2><SPAN
class=050230623-31032002>dictionary. deleting from the list doesn't affect the
</SPAN></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2><SPAN
class=050230623-31032002>dictionary underneath.</SPAN></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" size=2><SPAN
class=050230623-31032002></SPAN></FONT> </DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV><FONT face=Arial><FONT face="Courier New" size=2>>>> del
a<BR><SPAN class=050230623-31032002><FONT
color=#0000ff></FONT></SPAN></FONT></FONT></DIV></BLOCKQUOTE>
<DIV dir=ltr><FONT face=Arial><FONT face="Courier New" size=2><SPAN
class=050230623-31032002><FONT color=#0000ff>This deletes the name from the
underlying dictionary...</FONT></SPAN></FONT></FONT></DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV><FONT face=Arial><FONT face="Courier New"><FONT size=2><SPAN
class=050230623-31032002> </SPAN>>>> dir()<BR>['__builtins__',
'__doc__', '__name__', 'foo']</FONT></FONT></FONT></DIV></BLOCKQUOTE>
<DIV dir=ltr><FONT face=Arial><FONT face="Courier New"><FONT size=2><SPAN
class=050230623-31032002> </SPAN><SPAN class=050230623-31032002><FONT
color=#0000ff>so its gone from dir()
too... </FONT></SPAN></FONT><BR></DIV><FONT size=2></FONT></FONT>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px"></FONT>
<DIV><FONT size=2>But if you try to delete several items from a list, this
doesn't work,</FONT></DIV>
<DIV><FONT size=2>because of the difference between the string 'a' and
the name a, I think:</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2>>>> dir()<BR>['__builtins__', '__doc__',
'__name__', 'a', 'foo']<BR>>>> dir()[3:]<SPAN
class=050230623-31032002><FONT face="Courier New"
color=#0000ff> </FONT></SPAN><SPAN
class=050230623-31032002> </SPAN><BR>['a', 'foo']<BR>>>> for
name in dir()[3:]:<BR> del name<SPAN
class=050230623-31032002><FONT face="Courier New"
color=#0000ff> </FONT></SPAN></FONT></DIV>
<DIV><FONT size=2><SPAN
class=050230623-31032002> </SPAN></FONT></DIV></BLOCKQUOTE>
<DIV dir=ltr><FONT face="Courier New" color=#0000ff size=2><SPAN
class=050230623-31032002>This deletes the string that was in the list (I think,
</SPAN></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" color=#0000ff size=2><SPAN
class=050230623-31032002>I admit I'm getting more shaky
here...)</SPAN></FONT></DIV>
<BLOCKQUOTE dir=ltr
style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #0000ff 2px solid; MARGIN-RIGHT: 0px">
<DIV><FONT size=2>>>> for name in dir()[3:]:<BR>
del eval(name)<SPAN class=050230623-31032002><FONT face="Courier New"
color=#0000ff> </FONT></SPAN></FONT></DIV></BLOCKQUOTE>
<DIV dir=ltr><FONT size=2><SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff>This uses the string to eval which returns the
alue of </FONT></SPAN></FONT></DIV>
<DIV dir=ltr><FONT size=2><SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff>the variable named by the string - ie 3 in this
case.</FONT></SPAN></FONT></DIV>
<DIV dir=ltr><FONT size=2><SPAN class=050230623-31032002> </SPAN><BR><SPAN
class=050230623-31032002><FONT face="Courier New" color=#0000ff> <FONT
face="Times New Roman" color=#000000>> </FONT> </FONT></SPAN>So how do I
get acces to the Variable a via dir() ?<SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff> </FONT></SPAN></FONT></DIV>
<DIV dir=ltr><FONT size=2><SPAN
class=050230623-31032002></SPAN></FONT> </DIV>
<DIV dir=ltr><FONT size=2><SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff>I don't think you can. You have to go to the
Builtins </FONT></SPAN></FONT></DIV>
<DIV dir=ltr><FONT size=2><SPAN class=050230623-31032002><FONT
face="Courier New" color=#0000ff>dictionary and grub about in the innards - not
recommended!</FONT> </SPAN></FONT></DIV>
<DIV dir=ltr><FONT face="Courier New" color=#0000ff size=2><SPAN
class=050230623-31032002></SPAN></FONT> </DIV>
<DIV dir=ltr><FONT face="Courier New" color=#0000ff size=2><SPAN
class=050230623-31032002>Alan G.</SPAN></FONT></DIV>
<DIV dir=ltr><FONT size=2><SPAN
class=050230623-31032002></SPAN></FONT> </DIV></BODY></HTML>
------_=_NextPart_001_01C1D90A.1CCDBC30--