New submission from Sylwester Warecki: Hi!
An example below shows how differently local variables are treated in case of simple variables and classes. In case of objects the variable refers to global object, in case of simple number - it refers to a local. Example 2 shows worse case scenario. >From the text alone of the test2() function it is impossible to forsee its behavior (!). If there is no global variable 'a' then it crashes although without declaration of 'a' as a global it runs fine with the '+' operator on 'a'. However that will not be the case IF test3() is invoked. That is illogical. Example 3 shows the worst case scenario. functions z0 and z1 differ only by the variable name on the left side of the equation. In first case a is understood as global in second as local. What makes no sense is that the right side of equation does not change, however its interpretation is based on the left side of the equation. Example 4 This is an alteration of example 3 that shows the problem in full swing. 2 functions differ only by 2 last lines and yet they interpret the variables in completely different way. In other words, adding a line to end the code AFFECTS its beginning. That is least to say bizzaare. .................. I tried to keep examples simple - hopefully that will help. Regards, Sylwester ================================================================= class cheat: def __init__( self ): self.q = 4 a = cheat( ) b = 3 def test(): a.q = 22 b = 65 print "test a=", a.q print "test b=", b print "a=", a.q print "b=", b test() print "a=", a.q print "b=", b ================================= example 2 class cheat2: def __init__( self ): self.q = 4 def __add__( self, val ): self.q += val a=cheat2() def test2(): c = a + 4 print a.q def test3(): a += 4 test2() test3() =================================================== example 3 def z0(): c = a + 2 def z1(): a = a + 2 a = 10 z0() z1() ================================================================= example 4 def t1(): print a x=a+2 print x An example below shows how differently local variables are treated in case of simple variables and classes. In case of objects the variable refers to global object, in case of simple number - it refers to a local. Example 2 shows worse case scenario. >From the text alone of the test2() function it is impossible to forsee its behavior (!). If there is no global variable 'a' then it crashes although without declaration of 'a' as a global it runs fine with the '+' operator on 'a'. However that will not be the case IF test3() is invoked. That is illogical. Example 3 shows the worst case scenario. functions z0 and z1 differ only by the variable name on the left side of the equation. In first case a is understood as global in second as local. What makes no sense is that the right side of equation does not change, however its interpretation is based on the left side of the equation. Example 4 This is an alteration of example 3 that shows the problem in full swing. 2 functions differ only by 2 last lines and yet they interpret the variables in completely different way. In other words, adding a line to end the code AFFECTS its beginning. That is least to say bizzaare. .................. I tried to keep examples simple - hopefully that will help. Regards, Sylwester ================================================================= class cheat: def __init__( self ): self.q = 4 a = cheat( ) b = 3 def test(): a.q = 22 b = 65 print "test a=", a.q print "test b=", b print "a=", a.q print "b=", b test() print "a=", a.q print "b=", b ================================= example 2 class cheat2: def __init__( self ): self.q = 4 def __add__( self, val ): self.q += val a=cheat2() def test2(): c = a + 4 print a.q def test3(): a += 4 test2() test3() =================================================== example 3 def z0(): c = a + 2 def z1(): a = a + 2 # crash will happen here a = 10 z0() z1() ================================================================= example 4 def t1(): print a x=a+2 print x def t2(): print a #crash will happen here x=a+2 print x a=a+4 print a t1() t2() # this one will crash! ---------- components: Interpreter Core messages: 63261 nosy: swarecki severity: normal status: open title: Inconsistent variable lookup versions: Python 2.5 __________________________________ Tracker <[EMAIL PROTECTED]> <http://bugs.python.org/issue2237> __________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com