[Python-ideas] sys.py3k

Chris Angelico rosuav at gmail.com
Mon Nov 5 02:36:59 EST 2012


On Mon, Nov 5, 2012 at 9:33 AM, Steven D'Aprano <steve at pearwood.info> wrote:
> On 05/11/12 08:49, anatoly techtonik wrote:
>>
>> if sys.py3k:
>>    # some py2k specific code
>>    pass
>
> # Bring back reload in Python 3.
> try:
>     reload
> except NameError:
>     from imp import reload
>
> try:
>     any
> except NameError:
>     # Python 2.4 compatibility.
>     def any(items):
>         for item in items:
>             if item:
>                 return True
>         return False

Take the best of both worlds:

try: # py3k
    reload
except NameError:
    from imp import reload

Now you can grep your code for py3k without changing the language!

Never underestimate the value of comment tokens. Universal ones like
TODO or private ones like NULLSAFE, all it takes is grep or your
editor's Find function to make them all obvious.

ChrisA



More information about the Python-list mailing list