Skip to content

Change three occurences of xrange() to range() for compatibility with…#2

Open
warrickball wants to merge 1 commit intotimothydmorton:masterfrom
warrickball:python3
Open

Change three occurences of xrange() to range() for compatibility with…#2
warrickball wants to merge 1 commit intotimothydmorton:masterfrom
warrickball:python3

Conversation

@warrickball
Copy link
Copy Markdown

I just tried to run this basic example on my system under Python 3, which I called test_pymacula.py:

#!/usr/bin/env python3

import numpy as np
import matplotlib.pyplot as pl
import pymacula

t = np.arange(0, 500, 0.05)
model = pymacula.MaculaModel()
pl.plot(t, model(t))
pl.show()

and ran into the error

Traceback (most recent call last):
  File "test_pymacula.py", line 8, in <module>
    model = pymacula.MaculaModel()
  File "/home/wball/pypi/pymacula/pymacula/macula.py", line 179, in __init__
    self.spots = [Spot() for i in xrange(nspots)]
NameError: name 'xrange' is not defined

So I replaced three occurences of xrange to range. This gets the code running under Python 3. I haven't tested under Python 2 but as long as the number of spots is small I don't imagine there's a problem. If there is and you want to preserve Python 2 compatibility we can use a trick like

try:
    range = xrange
except NameError:
    pass

or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant