How do you programmatically set an attribute?

后端 未结 4 831
北荒
北荒 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:38

    let x be an object then you can do it two ways

    x.attr_name = s 
    setattr(x, 'attr_name', s)
    

提交回复
热议问题