Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6613231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:11:37+00:00 2026-05-25T20:11:37+00:00

One of my classes requires assignments to be completed in Python, and as an

  • 0

One of my classes requires assignments to be completed in Python, and as an exercise, I’ve been making sure my programs work in both Python 2 and Python 3, using a script like this:

#!/bin/bash
# Run some PyUnit tests
python2 test.py
python3 test.py

One thing I’ve been doing is making range work the same in both versions with this piece of code:

import sys

# Backport Python 3's range to Python 2 so that this program will run
# identically in both versions.
if sys.version_info < (3, 0):
    range = xrange

Is this a bad idea?

EDIT:

The reason for this is that xrange and range work differently in Python 2 and Python 3, and I want my code to do the same thing in both. I could do it the other way around, but making Python 3 work like Python 2 seems stupid, since Python 3 is “the future”.

Here’s an example of why just using range isn’t good enough:

for i in range(1000000000):
    do_something_with(i)

I’m obviously not using the list, but in Python 2, this will use an insane amount of memory.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:11:38+00:00Added an answer on May 25, 2026 at 8:11 pm

    You can use the six package which provides a Python 2 and 3 compatibility library and written by one of the Python core developers. Among its features is a set of standard definitions for renamed modules and functions, including xrange -> range. The use of six is one of many recommendations in the official Porting Python 2 Code to Python 3 HOWTO included in the Python Documentation set.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using System.Timers.Timer class in one of the classes in my application. I
I have an Ant build script to generate some classes. The process requires an
One of classes in my program uses some third-party library. Library object is a
I have two classes one of which inherits from the other. Im trying to
I'm executing the java binary from one of my classes, and a ClassNotFoundException gets
I have a field for addedBy in one of my classes. I've set up
Trying to override a tostring in one of my classes. return string.Format(@ name =
I need to validate a certain property to one of my classes. When I
I'm writing a website that will sell items from one of my classes. It
We recently had a code review . One of my classes was used so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.