Raising an exception on updating a 'constant' attribute in python

前端 未结 5 1439
逝去的感伤
逝去的感伤 2021-01-14 15:29

As python does not have concept of constants, would it be possible to raise an exception if an \'constant\' attribute is updated? How?

class MyClass():
            


        
5条回答
  •  余生分开走
    2021-01-14 16:22

    Start reading this:

    http://docs.python.org/reference/datamodel.html#customizing-attribute-access

    You basically write your own version of __setattr__ that throws exceptions for some attributes, but not others.

提交回复
热议问题