Python: Any way to declare constant parameters?

蹲街弑〆低调 提交于 2019-12-05 15:36:40

问题


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


回答1:


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

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




回答2:


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?



来源:https://stackoverflow.com/questions/3081464/python-any-way-to-declare-constant-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!