How to Access Function variables in Another Function

后端 未结 5 1781
梦如初夏
梦如初夏 2020-12-10 09:29

I have a small issue while calling multiple variables in python one function to another. Like I have to access the variable of xxx() variables in yyy(). Help me to do this.?

5条回答
  •  佛祖请我去吃肉
    2020-12-10 10:11

    Try this:

    def xxx():
        a=10
        b=15
        c=20
        return a, b
    
    def yyy():
        a, b = xxx()
        print a
        print b
    
    yyy()
    

    Or this:

    def xxx():
        global a, b
        a=10
        b=15
        c=20
    
    def yyy():
        print a
        print b
    
    xxx()
    yyy()
    

    For more info, use help('return') and help('global'), or see this and this question.

    And only use global when you need the variable can use at everywhere or you need the function return other things. Because modifying globals will breaks modularity.

提交回复
热议问题