updating a variable / scope problem
me at privacy.net
Fri Jul 9 15:45:10 CEST 2004
"Robert Brewer" <fumanchu at amor.org> wrote in
news:mailman.142.1089323574.5135.python-list at python.org:
> Rajarshi Guha wrote:
>> 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.
> If extend *modifies* c, then making it an argument to extend won't work;
> making it an argument makes a new binding for c, local to extend(), and
> won't modify c in clique(). The hack that used a list will work, but is
> still ugly. I'd suggest either rewriting the algorithm in register form
> (using a while loop) instead of recursive form, or (last resort) make c
> a global instead of a local within clique(). Does c get modified by
> clique()? If the answer is no, you might be able to rewrite extend() as
> a generator instead of a recursive function.
If extend *modifies* c, then it surely isn't too hard to do this:
def clique( .. ):
def extend(c, .. ):
if ( .. ) c = extend(c, .. )
c = 0
c = extend(c, .. )
This way it is obvious that you are modifying c in the inner function.
Alternatively how about using a class?
More information about the Python-list