injecting "set" into 2.3's builtins?

S

Skip Montanaro

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
except NameError:
from sets import Set as set
else:
del x

Of course, while it's transparent at one level (where sets are used) it's
not really transparent at another level (where the set object is defined).

I'm toying with the idea of adding this to the sitecustomize module at work:

import sets
import __builtin__
__builtin__.set = sets.Set
del sets, __builtin__

I'm wondering if others have tried it. If so, did it cause any problems?
I've not noticed any incompatibilities between the 2.3 and 2.4 versions of
set objects, but my use of them has been pretty straightforward.

Thanks,

Skip
 
A

and-google

Skip said:
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
clause.)
__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 siteglobals.py. Then
any modules or other non-site scripts could continue without the
polluted builtin scope.
 
J

John Machin

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


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.

Aarrgghh! When there's a documented API (sys.version_info) for
determining the version, such a module (and it's author) could be
charitably described as "broken". I can't imagine there are any on
Skip's site :)
 
S

Skip Montanaro

John> Aarrgghh! When there's a documented API (sys.version_info) for
John> determining the version, such a module (and it's author) could be
John> charitably described as "broken". I can't imagine there are any on
John> Skip's site :)

Thanks for the vote of confidence (I think). Yes, I'd only inject "set"
into builtins on 2.3, and anyone who wants to do version-dependent things
should use some variant of sys.version_info.

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top