Introspection
Miki
miki.tebeka at gmail.com
Wed Jan 6 10:44:31 EST 2010
Hello Martin,
> I'm looking for a way to make a list of string literals in a class.
from inspect import getsourcelines
from tokenize import generate_tokens, STRING, NUMBER
def is_literal(t):
return t[0] in (STRING, NUMBER)
def get_lieterals(obj):
lines, _ = getsourcelines(obj)
readline = iter(lines).next
return [t[1] for t in generate_tokens(readline) if is_literal(t)]
if __name__ == "__main__":
class A:
def f(self):
print "A", "B"
print get_lieterals(A)
HTH,
--
Miki
More information about the Python-list
mailing list