Are there any declaration keywords in Python?

前端 未结 5 1962
爱一瞬间的悲伤
爱一瞬间的悲伤 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 11:01

    I just realized there's a more direct answer too:

    x = 5
    
    def test():
        print 'x' in globals()
    
    if __name__ == "__main__":
        test()
    

    So if 'variablename' in globals(): the statement is an assignment otherwise the statement is a declaration

提交回复
热议问题