Python: Any way to declare constant parameters?
Asked Answered
L

2

14

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".

Lira answered 21/6, 2010 at 0:13 Comment(4)
References are always const, but values are not (you can't change what bar points to, but if bar 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 what bar points to, but assuming you don't resort to such hackery the rule generally applies.Lalitta
@NickBastin: Wha? bar = "changed!" -- I just changed what bar 'points' to (is assigned to, to be correct).Roarke
@EthanFurman: Not exactly. My point is that you can't change what bar references in the parent stack frame - you can of course assign the name bar locally to hide the object you were passed in, but that object still exists and remains unchanged in the parent.Lalitta
Perhaps you can call it bar_constant or BAR. 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
H
6

If bar is an inmutable object, bar won't change during the function.

You can also create your own constant object. The recipe here.

Headforemost answered 21/6, 2010 at 0:18 Comment(2)
Note that intercepting modifications doesn't solve the problem on its own. How do you know that the client code won't encounter a ConstError? Only with sufficient testing.Carpetbagger
bar = 0 -- I just changed bar (the original object that bar was assigned to remains uneffected).Roarke
C
-7

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?

Carpetbagger answered 21/6, 2010 at 0:16 Comment(6)
It is often a useful matter of policy - you are informing the user about a limitation of your function. Of course, this doesn't make as much sense in Python as there aren't prototypes, and there's no static compiler to enforce the constraint either.Lalitta
The point is to help the programmer avoid making mistakes, to the extent possible. Otherwise we would all write everything in assembly.Dissertate
@Jeremy Friesner: In Python, the "correct" way to do this is to just write a comment then.Hellen
The problem with comments is that the language doesn't enforce them. So not only will they not detect or report the problem, they can serve to actively prevent a reader from seeing the problem, if the reader sees the comment and assumes it is true, when it isn't.Dissertate
@Jeremy Friesner: Why should Python allow declarations of immutability when it doesn't even allow declarations of type? Type declarations would also "help the programmer avoid mistakes," but it's shunned in Python, too. Rely on tests, not on declarations.Carpetbagger
Readers of users of that function can then be confident that the variables passed to that function won't be modified.Imperceptive

© 2022 - 2024 — McMap. All rights reserved.