Python Chain getattr as a string

后端 未结 2 1932
遇见更好的自我
遇见更好的自我 2021-01-02 22:34
import amara
def chain_attribute_call(obj, attlist):
    \"\"\"
    Allows to execute chain attribute calls
    \"\"\"
    splitted_attrs = attlist.split(\".\")
             


        
相关标签:
2条回答
  • 2021-01-02 23:15

    Just copying from Useful code which uses reduce() in Python:

    from functools import reduce
    reduce(getattr, "X.Y.Z".split('.'), doc)
    
    0 讨论(0)
  • 2021-01-02 23:16

    you could also use:

    from operator import attrgetter
    attrgetter('x.y.z')(doc)
    
    0 讨论(0)
提交回复
热议问题