How to iterate over function arguments

后端 未结 5 728
清歌不尽
清歌不尽 2020-12-29 01:40

I have a Python function accepting several string arguments def foo(a, b, c): and concatenating them in a string. I want to iterate over all function arguments

5条回答
  •  既然无缘
    2020-12-29 02:18

    Is this perhaps what you'd like?

    def foo(a, b, c):
        "SilentGhost suggested the join"
        ' '.join(i if i is not None else '' for i in vars().values())
    
    def bar(a,b,c): 
        "A very usefull method!"
        print vars()
        print vars().values()
    

    Notice the use of vars(), which returns a dict.

提交回复
热议问题