updating a variable / scope problem

Robert Brewer fumanchu at amor.org
Thu Jul 8 17:48:36 EDT 2004


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.


Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org



More information about the Python-list mailing list