This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author swarecki
Recipients swarecki
Date 2008-03-04.21:37:06
SpamBayes Score 0.005638389
Marked as misclassified No
Message-id <1204666628.06.0.657463410912.issue2237@psf.upfronthosting.co.za>
In-reply-to
Content
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!
History
Date User Action Args
2008-03-04 21:37:08swareckisetspambayes_score: 0.00563839 -> 0.005638389
recipients: + swarecki
2008-03-04 21:37:08swareckisetspambayes_score: 0.00563839 -> 0.00563839
messageid: <1204666628.06.0.657463410912.issue2237@psf.upfronthosting.co.za>
2008-03-04 21:37:07swareckilinkissue2237 messages
2008-03-04 21:37:06swareckicreate