injecting "set" into 2.3's builtins?

and-google at and-google at
Fri Mar 11 09:08:52 CET 2005

Skip Montanaro wrote:

> I use sets a lot in my Python 2.3 code at work and have been using
> this hideous import to make the future move to 2.4's set type
> transparent:

>     try:
>         x = set

(Surely just 'set' on its own is sufficient? This avoids the ugly else

>     __builtin__.set = sets.Set

> I'm wondering if others have tried it. If so, did it cause any
> problems?

I don't know of any specific case where it would cause problems but I'd
be very wary of this; certainly doing the same with True and False has
caused problems in the past. A module might sniff for 'set' and assume
it is running on 2.4 if it sees it, with unpredictable results if it
relies on any other 2.4 behaviour.

I'd personally put this at the top of local scripts:

  from siteglobals import *

Then put compatibility hacks like set and bool in Then
any modules or other non-site scripts could continue without the
polluted builtin scope.

Andrew Clover
mailto:and at

More information about the Python-list mailing list