[Python-Dev] Accessing globals without dict lookup

Guido van Rossum guido@python.org
Fri, 08 Feb 2002 16:19:54 -0500


> Guido van Rossum writes:
>  > Oops, I was indeed confused.  I think I meant this:
>  > 
>  >     def keys(self):
>  >         return [k for k, c in self.__dict.iteritems() if c.objptr is not NULL]
> 
> Was I not clear, or am I missing something entirely?

I'm guessing both. ;-)

> keys() needs
> *no* special treatment, but items() and values() do:
> 
> class celldict(object):
>     ...
> 
>     def keys(self):
>         return self.__dict.keys()

Wrong.  keys() *does* need special treatment.  If c.objptr is NULL,
the cell exists, but keys() should not return the corresponding key.
This is so that len(x.keys()) == len(x.values()), amongst other
reasons!

>     def items(self):
>         return [k, c.objptr for k, c in self.__dict.iteritems()
>                 if c.objptr is not NULL]
> 
>     def values(self):
>         return [c.objptr for c in self.__dict.itervalues()
>                 if c.objptr is not NULL]

Yes, these are correct.

--Guido van Rossum (home page: http://www.python.org/~guido/)