updating a variable / scope problem
Rajarshi Guha
rajarshi at presidency.com
Thu Jul 8 16:57:10 EDT 2004
On Thu, 08 Jul 2004 13:32:55 -0700, Robert Brewer wrote:
> Rajarshi Guha wrote:
>> def f(x):
>>
>> print 'hello'
>> c =0
>>
>> def g(y):
>> print c
>> c = c + 1
>> print 'bye'
>>
>> g(10)
>>
>> if __name__ == "__main__":
>> f(5)
>>
>> running it gives me an error:
>> UnboundLocalError: local variable 'c' referenced before assignment
>>
> Once you add the line "c = c + 1", you've told Python that c should be
> local to g(), since it involves a binding (assignment). It is *possible*
> to get around this with, for example:
>
Aah, thanks. I'm basically converting Bron and Kerbosch's clique detection
algorithm to python. The described algorithm is basically :
def clique( .. ):
def extend( .. ):
..
..
if ( .. ) extend( .. )
..
..
c = 0
..
extend( .. )
The variable c is accessed in the function extend().
I suppose I could just make c and argument to extend as you mention -
though it seems a little less elegent than allowing extend() to have
access to c directly.
Thanks,
Rajarshi
More information about the Python-list
mailing list