Are there any declaration keywords in Python?

前端 未结 5 2013
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-03 10:48

Are there any declaration keywords in python, like local, global, private, public etc. I know it\'s type free but how do you know if this statement:

x = 5;
         


        
5条回答
  •  無奈伤痛
    2021-02-03 10:57

    It's worth mentioning that there is a global keyword, so if you want to refer to the global x:

    x = 4
    def foo():
        x = 7 # x is local to your function
    

    You need to do this:

    x = 4
    def foo():
        global x # let python know you want to use the top-level x
        x = 7
    

提交回复
热议问题