Finding the variables (read or write)

Terry Reedy tjreedy at
Mon Jan 14 22:42:42 CET 2013

On 1/14/2013 4:28 PM, Chris Angelico wrote:
> On Tue, Jan 15, 2013 at 6:48 AM,  <servekarimi at> wrote:
>> I'd like to develop a small debugging tool for python programs.In Dynamic Slicing How can I find the variables that are accessed in a statement? And find the type of access (read or write) for those variables (in Python).
>> ### Write: A statement can change the program state.
>> ### Read : A statement can read the program state .
>> **For example in these 4 lines we have:
>> (1) x = a+b    => write{x} & read{a,b}
>> (2) y=6        => write{y} & read{}
>> (3) while(n>1) => write{} & read{n}
>> (4) n=n-1      => write{n} & read{n}

I would try compiling the source code to an ast (abstract syntax tree). 
See the ast module for how to do that and how to 'read' the resulting tree.

> An interesting question. What's your definition of "variable"? For
> instance, what is written and what is read by this statement:
> self.lst[2] += 4
> Is "self.lst" considered a variable? (In C++ etc, this would be a
> member function manipulating an instance variable.) Or is "self" the
> variable? And in either case, was it written to?

'self' is read, 'lst' is written to.

  What about:
> self.lst.append(self.lst[-1]+self.lst[-2])
> (which might collect Fibonacci numbers)?

'self' read, 'lst' read and written. Knowing that for non-builtins is 
another matter ;-).

Terry Jan Reedy

More information about the Python-list mailing list