Not with np.arange
, as that gives you a fully materialized array:
>>> r = np.arange(0., 1000000., 1)
>>> type(r)
<class 'numpy.ndarray'>
>>> r.__sizeof__()
8000048
So after it is created, it's simply too late. Also, it has no access to your stepsize
variable, so couldn't notice that you changed it.
Compare to range
, which is a lightway object that calculates values on the fly:
>>> r = range(0, 1000000, 1)
>>> type(r)
<class 'range'>
>>> r.__sizeof__()
24
(Note I'm using Python 3... you should really join us. Not just because in Python 2 the equivalent has the ugly name xrange
.)
Now... that range
object does have a step
attribute:
>>> dir(r)
['__bool__', '__class__', ..., 'count', 'index', 'start', 'step', 'stop']
But it won't let us change it:
>>> r.step = 2
Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
r.step = 2
AttributeError: readonly attribute
You could use your own range class supporting such a state change, though:
class Range:
def __init__(self, start, stop, step):
self.current = start
self.stop = stop
self.step = step
def __iter__(self):
while self.current < self.stop:
yield self.current
self.current += self.step
for i in (r := Range(0, 10, 1)):
print(i)
if i > 5:
print("change")
r.step = 0.5
Output:
0
1
2
3
4
5
6
change
6.5
change
7.0
change
7.5
change
8.0
change
8.5
change
9.0
change
9.5
change