In Python, why is list[] automatically global?

Viewed 13957

This is a weird behavior.

Try this :

rep_i=0
print "rep_i is" , rep_i
def test():
  global rep_i #without Global this gives error but list , dict , and others don't
  if rep_i==0:
    print "Testing Integer %s" % rep_i
    rep_i=1
  return "Done"

rep_lst=[1,2,3]
 

def test2():
  if rep_lst[0]==1:
    print "Testing List %s" % rep_lst
  return "Done"


if __name__=="__main__":
  test()
  test2()

Why list do not need to declare global? are they automatically global?

I find it really weird, I use list most of the time and I don't even use global at all to us them as global...

5 Answers
Related