Variable Scopes
Mon Dec 18, 2006 · 98 words
value = 0

def setValue():

    value = 1

setValue ()

print value

What's that gonna print? That's right - 0. Must use global instead:

value = 0

def setValue():

    global value

    value = 1

setValue ()

print value

It's actually the exact same with PHP:

$value = 0;

function setValue () { $value = 1; }

setValue ();

print ("value: " . $value);

My brain tells me that if something's been defined previously, then we should be referencing that thing instead. It's not like I'm explicitly using different namespaces in - and outside my function. Like with JavaScript.


back · essays · credits ·