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

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699
  • 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. – Nick Bastin Jun 21 '10 at 00:21
  • 2
    @NickBastin: Wha? `bar = "changed!"` -- I just changed what `bar` 'points' to (is assigned to, to be correct). – Ethan Furman Sep 30 '11 at 20:13
  • @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. – Nick Bastin Oct 01 '11 at 13:56
  • 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. – Joooeey Mar 14 '18 at 17:58

2 Answers2

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.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153
razpeitia
  • 1,947
  • 4
  • 16
  • 36
  • 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. – Nikhil Jun 21 '10 at 17:30
  • `bar = 0` -- I just changed bar (the original object that `bar` was assigned to remains uneffected). – Ethan Furman Sep 30 '11 at 20:08
-6

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?

Nikhil
  • 5,705
  • 1
  • 32
  • 30
  • 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. – Nick Bastin Jun 21 '10 at 00:22
  • 10
    The point is to help the programmer avoid making mistakes, to the extent possible. Otherwise we would all write everything in assembly. – Jeremy Friesner Jun 21 '10 at 00:38
  • @Jeremy Friesner: In Python, the "correct" way to do this is to just write a comment then. – carl Jun 21 '10 at 03:57
  • 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. – Jeremy Friesner Jun 21 '10 at 14:47
  • @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. – Nikhil Jun 21 '10 at 17:23
  • Readers of users of that function can then be confident that the variables passed to that function won't be modified. – Alaya Apr 25 '17 at 11:14