[Tutor] is this a bug global i ???

Alan G alan.gauld at freenet.co.uk
Tue Jun 14 19:45:04 CEST 2005


class A:
  def __init__(self,j):
    self.j = j

  def something(self):
    print self.j
    print i            # PROBLEM is here there is no var i in class A
but it works ???

if __name__ == '__main__':
  i = 10
  a = A(5)
  a.something()

> I don't define global i but it will takes var i from outside of
class A.
> Can somebody explain this ???

You only need to declare global if you are modifying data if you only
read it there is no need for explicit global statements.

Alan G.



More information about the Tutor mailing list