None assigment

Michael Hudson mwh21 at cam.ac.uk
Thu Feb 8 15:05:48 EST 2001


"Rainer Deyke" <root at rainerdeyke.com> writes:

> "Simon Brunning" <SBrunning at trisystems.co.uk> wrote in message
> news:mailman.981653763.25307.python-list at python.org...
> > > From: Gregoire Welraeds [SMTP:greg at perceval.be]
> > > > After doing "None = 2", you can "del None" to get the default value
> > > > back.
> >
> > Hmm. This is true - I didn't know that.
> >
> > > If we follow that logic, I could use any non assigned variable to have
> the
> > > following working:
> > >
> > > >>> a= [1,'',3]
> > > >>> filter(b,a)
> > >
> > > but this won't work as the interpreter complains that there is no
> variable
> > > named b.
> >
> > You can't do this with *ordinary* labels - by del'ing them, you are
> > un-assigning them. But the 'None' label is *extraordinary*, obviously.
> 
> Actually, it's just in '__builtins__'.  When you assign to 'None', you
> create a variable in the module namespace which hides '__builtins__.None'.
> When you delete it, the 'None' in '__builtins__' is revealed.  This applies
> to all labels:

It's not very often I come up against something I don't understand
in Python, but try this:

>>> del __builtins__ 

This makes Python, erm, interesting, to use:

>>> import sys
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: __import__ not found

but yet:

>>> None 
>>> 

i.e. None still gets found.

What's going on?  frameobject.c:172 is what's going on:

	if (builtins == NULL) {
		/* No builtins!  Make up a minimal one. */
		builtins = PyDict_New();
		if (builtins == NULL || /* Give them 'None', at least. */
		    PyDict_SetItemString(builtins, "None", Py_None) < 0) {
			Py_DECREF(f);
			return NULL;
		}
	}

Well, I though I'd share my little traipse through Python's innards
with you all...

Cheers,
M.

-- 
  Clue: You've got the appropriate amount of hostility for the
  Monastery, however you are metaphorically getting out of the 
  safari jeep and kicking the lions.                         -- coonec
               -- http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html



More information about the Python-list mailing list