Python Chain getattr as a string

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


        
2条回答
  •  梦毁少年i
    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)
    

提交回复
热议问题