Finding the variables (read or write)

Chris Angelico rosuav at gmail.com
Mon Jan 14 22:46:39 CET 2013


On Tue, Jan 15, 2013 at 8:37 AM, Chris Kaynor <ckaynor at zindagigames.com> wrote:
> And those aren't even covering the case that a, normally non-mutating,
> method actually mutates.

If it's static analysis, I'd quietly ignore those sorts of cases.
Anything can be changed any time, including stuff that's completely
unrelated to what you're working on.

Now, if the OP just wants to know what names get referenced (without
distinguishing reads from writes), that's quite possible, and in fact
easy - if you're willing to analyze a whole function instead of a
single statement.

>>> def foo():
	x=y+1

>>> foo.__code__.co_varnames
('x',)
>>> foo.__code__.co_names
('y',)

The first one is the ones that get assigned to (not quite the same as
"written to"), the second is ones that don't. Well, more or less. In
simple cases.

ChrisA



More information about the Python-list mailing list