Subclassed dict as globals

Terry Reedy tjreedy at udel.edu
Wed Jan 26 17:03:36 EST 2005


"Fredrik Lundh" <fredrik at pythonware.com> wrote in message 
news:ct8l51$2lp$1 at sea.gmane.org...
> Evan Simpson wrote:
>
>> In Python 2.4 the following works:
>>
>> >>> class G(dict):
>> ...   def __getitem__(self, k):
>> ...     return 'K' + k
>> ...
>> >>> g = G()
>> >>> exec 'print x, y, z' in g
>> Kx Ky Kz
>> >>>
>>
>> ...while in Python 2.3 it fails with NameError: name 'x' is not defined.

> I don't think he added this purely by accident,

As I remember, Raymond H.  was responding to a request by someone who ran 
into that exception.  In general, functions accepting a base class should 
at least accept subclasses of that baseclass.

tjr






More information about the Python-list mailing list