[Tutor] Random number selection
Steven D'Aprano
steve at pearwood.info
Tue Jan 3 11:57:50 CET 2012
Dave Angel wrote:
> Variables with integer values are not 'called." They also don't change
> values unless you change it. So if you've got a loop, and you want them
> to have different values each time, then give them different values each
> time, by calling a function that does return a different value.
In fairness, some programming communities call instance attributes "variables"
(which annoys me no end, but it is common), and computed attributes
("variables") can get a different value each time you look them up. So if you
can have this:
instance = MyClass()
instance.x # x is a computed "instance variable" (blah, I hate that term)
=> gives 23
instance.x
=> gives 42
then in principle it isn't a stupid idea to have this too:
x = MyVariable()
x
=> gives 23
x
=> gives 42
I don't know any language that operates like that, and it is probably a bad
idea in practice, but the principle isn't entirely crazy.
It's also an *incredibly* common thought -- I have seen so many people assume
that after doing this once:
x = random(1, 100)
x will now have a different random number each time you look at it. Which kind
of makes sense, if you think of x as a variable that varies on its own instead
of something which can be varied.
--
Steven
More information about the Tutor
mailing list