Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #65814

Re: Pylint across Python versions

References <317fd2a4-186b-4b97-ad7f-084609d0a9a4@googlegroups.com>
Date 2014-02-11 00:59 +1100
Subject Re: Pylint across Python versions
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.6621.1392040780.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Feb 10, 2014 at 10:39 PM,  <thomas.lehmann@adtech.com> wrote:
> Example:
> I'm using a construct like this:
>
> if sys.version.startswith("3."):
>     unicode = str
>
> The reason is that Python 3 does not have this
> function anymore but pylint yells for Python < 3
> about redefinition also it does not happen.
>
> How to get forward with this?

It's more common to spell that with a try/except. Does pylint complain
if you use this instead?

try:
    unicode
except NameError:
    unicode = str

Although it would be better to write your code for Python 3, and have
compatibility code at the top for Python 2. That would mean using
'str' everywhere, and then having this at the top:

try:
    str = unicode
except NameError:
    pass

That way, when you're ready to drop support for Python 2, you simply
delete the compat code and everything works. Otherwise, you have to
maintain messy code indefinitely.

Alternatively, to avoid redefinition at all, you could use your own
name everywhere:

try:
    unicode_string = unicode
except NameError:
    unicode_string = str

Use something less unwieldy if you prefer, but this avoids any chance
of collision :)

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Pylint across Python versions thomas.lehmann@adtech.com - 2014-02-10 03:39 -0800
  Re: Pylint across Python versions Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-02-10 13:03 +0000
  Re: Pylint across Python versions Chris Angelico <rosuav@gmail.com> - 2014-02-11 00:59 +1100
  Re: Pylint across Python versions Ned Batchelder <ned@nedbatchelder.com> - 2014-02-10 10:36 -0500

csiph-web