Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytes == str
#1
bytes == str is always True on python2 and always False on python3.  so i have been using it for version testing, at least for major version numbers.  one problem is that it is obscure code; the meaning isn't obvious just looking at that alone.
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
you can use sys.version_info
Output:
Python 3.6: sys.version_info     A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is (2, 0, 0, 'final', 0). The components can also be accessed by name, so sys.version_info[0] is equivalent to sys.version_info.major and so on.     Changed in version 3.1: Added named component attributes. Python 2.7: sys.version_info     A tuple containing the five components of the version number: major, minor, micro, releaselevel, and serial. All values except releaselevel are integers; the release level is 'alpha', 'beta', 'candidate', or 'final'. The version_info value corresponding to the Python version 2.0 is (2, 0, 0, 'final', 0). The components can also be accessed by name, so sys.version_info[0] is equivalent to sys.version_info.major and so on.     New in version 2.0.     Changed in version 2.7: Added named component attributes
Reply
#3
yes i had used if sys.version_info.major < 3: but if bytes == str: seemed to not need to import anything.  i could do the version test to decide which of two big import statements to use.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
You can import just the version_info if you don't want all the sys module. After all, it is part of Python so the import statement is not such a big deal.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
i was thinking more in terms of doing the test before importing anything.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

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