I have a method:
def foo(bar):
# ...
Is there a way to mark bar
as constant? Such as "The value in bar
cannot change" or "The object pointed to by bar
cannot change".
I have a method:
def foo(bar):
# ...
Is there a way to mark bar
as constant? Such as "The value in bar
cannot change" or "The object pointed to by bar
cannot change".
If bar is an inmutable object, bar won't change during the function.
You can also create your own constant object. The recipe here.
bar = 0
-- I just changed bar (the original object that bar
was assigned to remains uneffected). –
Roarke No.
What's the point? If you're writing the function, isn't it up to you to make sure bar
doesn't change? Or if you're calling the function, who cares?
© 2022 - 2024 — McMap. All rights reserved.
const
, but values are not (you can't change whatbar
points to, but ifbar
is mutable you can still change its' value). Of course, this isn't really true, since you can diddle with your parents' stack frame and change whatbar
points to, but assuming you don't resort to such hackery the rule generally applies. – Lalittabar = "changed!"
-- I just changed whatbar
'points' to (is assigned to, to be correct). – Roarkebar
references in the parent stack frame - you can of course assign the namebar
locally to hide the object you were passed in, but that object still exists and remains unchanged in the parent. – Lalittabar_constant
orBAR
. That should signify to anyone looking at your code that they shouldn't re-assign that name. Hence, you've marked it as a constant. You can't really do much more than that. – Pus