[Tutor] idle - namespaces (was idle)

Gregor Lingl glingl@aon.at
Sun, 31 Mar 2002 22:44:06 +0200


This is a multi-part message in MIME format.

------=_NextPart_000_0041_01C1D905.907F24C0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

From: john public=20
To: tutor@python.org=20
I created a module( I think I am using the right word ) in Idle called =
trash.py, I use it for my throw 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?  John

I find this an interesting question. (Although it probably
does not concern only IDLE but will also occur when working
with the interactive interpreter)=20
If you start IDLE, you can look for existing names:

>>> dir()
['__builtins__', '__doc__', '__name__']
>>> __name__
'__main__'
>>>=20

If you have, say, the following in trash:

a =3D 3

def foo(x):
    return x*x

and then run trash, you will find:

>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'foo']
>>>=20

Now you can

>>> del a
>>> dir()
['__builtins__', '__doc__', '__name__', 'foo']
>>>=20


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

=20
>>> dir()
['__builtins__', '__doc__', '__name__', 'a', 'foo']
>>> for name in dir()[3:]:
    del eval(name)
=20
Apparently eval('a') returns 3 and not a.
So how do I get acces to the Variable a via dir() ?

These reveals that there is much very mysterious to me.

Moreover if I do not run but import trash, I find:

>>> dir()
['__builtins__', '__doc__', '__name__', 'trash']
>>> dir(trash)
['__builtins__', '__doc__', '__file__', '__name__', 'a', 'foo']
>>>=20

What is this exactly?

Does=20
>>> del trash=20
>>>

now delete the names a and foo?
=20
Hopefully this thread may reveal a little bit of the inner working
as well as management of namespaces.

Gregor

P.S.: I remember that in Logo (in acient times) it was very easy to =
delete
names


------=_NextPart_000_0041_01C1D905.907F24C0
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2600.0" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><B>From:</B> <A title=3Dapython101@yahoo.com=20
href=3D"mailto:apython101@yahoo.com">john public</A> </DIV>
<DIV style=3D"FONT: 10pt arial">
<DIV><B>To:</B> <A title=3Dtutor@python.org=20
href=3D"mailto:tutor@python.org">tutor@python.org</A> <BR>I created a =
module( I=20
think I am using the right word ) in Idle called trash.py, I use it for =
my throw=20
away code. However I just noticed something, when I delete all the code =
to write=20
some more throw away code the variables and I think also other things do =
not get=20
deleted. How do I delete them?&nbsp;&nbsp;John</DIV></DIV>
<DIV><FONT size=3D2></FONT><FONT face=3DArial><FONT face=3D"Courier New" =

size=3D2></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" =
size=3D2></FONT></FONT><FONT=20
face=3DArial><FONT face=3D"Courier New" size=3D2>I find this an =
interesting question.=20
(Although it probably</FONT></FONT></DIV>
<DIV><FONT size=3D2>does not concern only IDLE but will also occur when=20
working</FONT></DIV>
<DIV><FONT size=3D2>with the interactive interpreter)</FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>If you start IDLE, you can look for existing=20
names:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2></FONT><FONT face=3DArial><FONT face=3D"Courier New" =

size=3D2>&gt;&gt;&gt; dir()<BR>['__builtins__', '__doc__',=20
'__name__']<BR>&gt;&gt;&gt; __name__<BR>'__main__'<BR>&gt;&gt;&gt;=20
</FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" =
size=3D2></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" size=3D2>If you have, =
say, the=20
following in trash:</FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" =
size=3D2></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" size=3D2>a =3D =
3</FONT></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" size=3D2>def=20
foo(x):<BR>&nbsp;&nbsp;&nbsp; return x*x</FONT></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" size=3D2>and then run =
trash, you=20
will find:</FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" =
size=3D2></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" size=3D2>&gt;&gt;&gt; =

dir()<BR>['__builtins__', '__doc__', '__name__', 'a', =
'foo']<BR>&gt;&gt;&gt;=20
</FONT></FONT></DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" =
size=3D2></FONT></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" size=3D2>Now you=20
can</FONT></FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial><FONT face=3D"Courier New" size=3D2>&gt;&gt;&gt; =
del=20
a<BR>&gt;&gt;&gt; dir()<BR>['__builtins__', '__doc__', '__name__',=20
'foo']<BR>&gt;&gt;&gt; </DIV>
<DIV><BR></DIV></FONT></FONT>
<DIV><FONT size=3D2>But if you try to delete several items from a list, =
this=20
doesn't work,</FONT></DIV>
<DIV><FONT size=3D2>because of the difference between the&nbsp;string =
'a' and the=20
name a, I think:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&gt;&gt;&gt; dir()<BR>['__builtins__', '__doc__', =
'__name__',=20
'a', 'foo']<BR>&gt;&gt;&gt; dir()[3:]<BR>['a', 'foo']<BR>&gt;&gt;&gt; =
for name=20
in dir()[3:]:<BR>&nbsp;&nbsp;&nbsp;&nbsp; del name</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>&nbsp;<BR>&gt;&gt;&gt; dir()<BR>['__builtins__', =
'__doc__',=20
'__name__', 'a', 'foo']<BR>&gt;&gt;&gt; for name in=20
dir()[3:]:<BR>&nbsp;&nbsp;&nbsp; del eval(name)<BR>&nbsp;<BR>Apparently=20
eval('a') returns 3 and not a.</FONT></DIV>
<DIV><FONT size=3D2>So how do I get acces to the Variable a via dir()=20
?</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>These reveals that there is much very mysterious to=20
me.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Moreover if I do not run but import trash, I=20
find:</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>&gt;&gt;&gt; dir()<BR>['__builtins__', '__doc__', =
'__name__',=20
'trash']<BR>&gt;&gt;&gt; dir(trash)<BR>['__builtins__', '__doc__', =
'__file__',=20
'__name__', 'a', 'foo']<BR>&gt;&gt;&gt; </FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>What is this exactly?</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Does </FONT></DIV>
<DIV><FONT size=3D2>&gt;&gt;&gt; del trash </FONT></DIV>
<DIV><FONT size=3D2>&gt;&gt;&gt;</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>now delete the names a and foo?</FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT size=3D2>Hopefully this thread may reveal a little bit of the =
inner=20
working</FONT></DIV>
<DIV><FONT size=3D2>as well as management of namespaces.</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>Gregor</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2>P.S.: I remember that in Logo (in acient times) it =
was very=20
easy to delete</FONT></DIV>
<DIV><FONT size=3D2>names</FONT></DIV>
<DIV><FONT size=3D2></FONT>&nbsp;</DIV></BODY></HTML>

------=_NextPart_000_0041_01C1D905.907F24C0--