How do I get an attribute from an object if I have the attribute name in a string?

前端 未结 2 1721
我在风中等你
我在风中等你 2020-12-12 06:01
 class a :
     b = 10

I have a class \'a\' with attribute \'b\'.

  o = a()

I create a object \'o\' of class \'a\

相关标签:
2条回答
  • 2020-12-12 06:12

    Use the built-in getattr function:

    getattr(o, v)
    
    0 讨论(0)
  • 2020-12-12 06:25

    Are you looking for something like this?

    getattr(o, v)
    
    0 讨论(0)
提交回复
热议问题