How do you programmatically set an attribute?

后端 未结 4 906
北荒
北荒 2020-11-22 04:41

Suppose I have a python object x and a string s, how do I set the attribute s on x? So:

>>> x =          


        
4条回答
  •  野的像风
    2020-11-22 05:36

    Also works fine within a class:

    def update_property(self, property, value):
       setattr(self, property, value)
    

提交回复
热议问题