Scopes and exceptions
Carsten Geckeler
nospam at no.spam
Fri Dec 15 12:54:47 EST 2000
On Fri, 15 Dec 2000, Hans Nowak wrote:
> Magnus Heino wrote:
> >
> > Could someone please explain this to me:
> >
> > [magnus at daysleeper magnus]$ cat e.py
> >
> > a = 'test'
> > b = 'test'
> >
> > print 'a = %s' % id(a)
> > print 'b = %s' % id(b)
> >
> > def func():
> > print "'test' = %s" % id('test')
> >
> > func()
> >
> > try:
> > test, '%s' % (a,)
> > except NameError, e:
> > print 'e = %s' % id(e)
> > print 'a = %s' % id(a)
> > print 'b = %s' % id(b)
> > print "'test' = %s" % id('test')
> >
> > [magnus at daysleeper magnus]$ python e.py
> > a = 136227368
> > b = 136227368
> > 'test' = 136227368
> > e = 135997204
> > a = 136227368
> > b = 136227368
> > 'test' = 136227368
> > [magnus at daysleeper magnus]$
>
> I don't understand what is so special about this? Python stores a string
> 'test' in memory... a points to this string, then b, so id(a), id(b) and
> id('test') all yield the same result. This is also the case after the
> exception. Variable e has a different id, because it's a different
> variable; not a string, but an instance of exceptions.NameError.
It _is_ a string, but it's not 'test' but
"There is no variable named 'test'"
therefore the ids are different.
But the point of Magnus' mail was if
id(a) == id('%s' % (a,))
and it is *not*.
Cheers, Carsten
--
Carsten Geckeler: carsten dot geckeler at gmx dot de
To get proper email-address replace `dot' and `at' by the corresponding symbols.
More information about the Python-list
mailing list