attribute lookup str and objects like object.myvar

前端 未结 1 1734
滥情空心
滥情空心 2021-01-29 11:17

I want to know how i can concat object fields with a variable, it\'s hard to explain to me, let me give an example

Example:

My object have:

myobj         


        
相关标签:
1条回答
  • 2021-01-29 11:58

    I'm guessing you are looking for getattr:

    print getattr(myobject, some_dumb_field)
    

    This will look up the attribute of myobject whose name is given by the string some_dumb_field.

    For example,

    getattr(myobject, 'name')
    

    is equivalent to

    myobject.name
    

    (Warning: This could be called attribute lookup; you won't find it under the term "concat".)

    0 讨论(0)
提交回复
热议问题