Python: Any way to declare constant parameters?

2019-04-06 00:35发布

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

2条回答
再贱就再见
2楼-- · 2019-04-06 01:11

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

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

查看更多
Fickle 薄情
3楼-- · 2019-04-06 01:20

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?

查看更多
登录 后发表回答