Bitten by my C/Java experience
Tobiah
toby at tobiah.org
Mon May 4 13:18:47 EDT 2015
On 05/04/2015 08:20 AM, Cecil Westerhof wrote:
> Potential dangerous bug introduced by programming in Python as if it
> was C/Java. :-(
> I used:
> ++tries
> that has to be:
> tries += 1
>
> Are there other things I have to be careful on? That does not work as
> in C/Java, but is correct syntax.
>
One surprise for the new user is an otherwise handy rule of scope.
A variable in a function will by default access any global variables of
the same name *unless* it is assigned to in the function.
def glob():
print "global:", foo
def loc():
foo = 2
print "local:", foo
def alt():
global foo
foo = 1
print "altered:", foo
foo = 3
glob()
print "Original:", foo
loc()
print "Original:", foo
alt()
print "Original:", foo
################# Output ##################
global: 3
Original: 3
local: 2
Original: 3
altered: 1
Original: 1
More information about the Python-list
mailing list