Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
in python 2 and python 3
#1
in python 2 and python 3 i want to test if a variable refers to any kind of string.  in python 2 i can do if isinstance( var, (str,unicode) ): or if isinstance( var, basestring ): and in python 3 i can do if isinstance( var, str ): but i want to have one coding that works in python 2 and in python 3 without installing anything extra (so someone can just download this code and run it as-is).

i'd also like to find a way to test for any integer (in python 2, including long).

it seems like i will need to test for the python version somehow.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
If you want to write code that works on both pyzhon 2 and python 3, you should be using six
In this particular case, six.string_types is what you want.

If you really want to do it without using third-party modules, you will have to manually check for the version, and use if/else blocks to run the appropriate code every time you use something that is different between the two versions.
IMO, it's not worth it at all.
Reply
#3
If you check in the beginning of the code the Python version and do something similar to this:

if sys.version_info[0] == 2:
    range = xrange
Maybe inherit the unicode class in str class in Python 2 and predefine __str__ and __repr__ so awlays to return Unicode. I might be not so clear but I just start to learn classes in Python. Blush
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
Quote:it seems like i will need to test for the python version somehow.

I will often do this for my games, as people usually do not give it a second chance or want to hassle much before giving up on your game and moving on. So i usually make sure my programs runs in both py2 and py3.

You can also define a function based on which version the code is being ran under. And example of importing different module renames and defining a the same function under both versions. You can put this stuff in the beginning of your script, or if its a large program, i usually make a separate module for version checking and version definitions
import sys
if sys.version[0] == '3':
    from urllib.request import urlopen
    def do_something():
        #do something via py3 method
        pass 
else:
    from urllib2 import urlopen
    range = xrange 
    def do_something()
        #do something via py2 method
        pass 
Recommended Tutorials:
Reply
#5
You can often manage this without importing sys, depending on what you're doing and how complicated it is:

try:
    input = raw_input
except NameError:
    pass
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
As mention six is more elegant than import sys  try: except stuff.
A lot is under six.moves.
Also use future then can take quite a lot.
from __future__ import absolute_import, division, print_function, unicode_literals
from six.moves import input
from six.moves.urllib.request import urlopen
from six.moves import range
I think is best to not write code for Python 2.x anymore.
To move faster forward,Python 3.6 aslo has a lot features that can not be back ported.
Django 2.0 will drop Python 2.7 and 3.4,i think this is the right move.
Quote:Django 2.0 supports Python 3.5+. Since Django 1.11, support for Python 2.7 and 3.4 is removed.
Reply
#7
the script people download needs to work without them downloading or installing anything else, nor having done so in the past, whether they run it on a recent python 2 or run it on a recent python 3.  importing sys and testing sys.version_info is very reasonable to do.  i have done the range = xrange thing wavic suggested, before.  i have no problem with that, either.  but i don't understand ichabod801's suggestion.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Quote:the script people download needs to work without them downloading or installing anything else, nor having done so in the past, whether they run it on a recent python 2 or run it on a recent python 3.
the module six, and sys are in the standard library so they wont have to install anything extra either way.


Quote:but i don't understand ichabod801's suggestion.
he is saying to utilize python3.x where raw_input is no longer defined to determine python version

(Feb-12-2017, 11:55 PM)ichabod801 Wrote: You can often manage this without importing sys, depending on what you're doing and how complicated it is:

try:
    input = raw_input
except NameError:
    pass


python 2.x
metulburr@ubuntu:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> input = raw_input
>>>
python3.x
metulburr@ubuntu:~$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> input = raw_input
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'raw_input' is not defined
>>> 
in py2 both input and raw_input are defined, whereas py3 only input is. So if you catch a NameError, it will pass in both versions, but it gives you an option to throw version specific code in the try/except block. 

try:
    input = raw_input
    #your py2 code here
except NameError:
    #your py3 code here
*Of course if you get a NameError with whatever py2 specific code you have, then that will mess things up
Recommended Tutorials:
Reply
#9
@ichabod801 suggested what I did but in try statement. "Easier to ask for forgiveness than permission"
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#10
(Feb-13-2017, 04:21 AM)metulburr Wrote: he is saying to utilize python3.x where raw_input is no longer defined to determine python version

Well, not exactly. What I meant to say was that in some cases, you don't need sys to determine the version. In that case I had a script that I wanted to work in either 2.x or 3.x, but I wanted to use the 3.x version of input (which is 2.x's raw_input) no matter what. I wasn't suggesting that you use raw_input's existence as a way to test for version if you wanted to do something else across the 2/3 barrier.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020