By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

injecting "set" into 2.3's builtins?

P: n/a

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

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
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.

--
Andrew Clover
mailto:an*@doxdesk.com
http://www.doxdesk.com/

Jul 18 '05 #2

P: n/a

an********@doxdesk.com wrote:
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 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.


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 :-)

Jul 18 '05 #3

P: n/a
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.


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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.