Regarding the python module symtable -


i'm working on plugin sort of class browser python codes. found python module symtable helpful. however, seems module has drawback. cannot instance attributes using module. here's example python code in file named src.py:

import os  class a():     cl_attr = 1     def fn1(self):         self.attr1 = 'hello'     def fn2(self, arg):         self.attr2 = arg 

then in python interpreter:

python 2.7 (r27:82500, jul  9 2010, 09:28:49)  [gcc 4.1.1] on linux2 type "help", "copyright", "credits" or "license" more information. >>> src = open('src.py', 'r').read() >>> import symtable >>> table = symtable.symtable(src, 'src.py', 'exec') >>> table.get_children()[0].get_identifiers() ['fn1', 'cl_attr', 'fn2'] 

the above results indicate class attribute cl_attr returned. however, when try:

>>> table.get_children()[0].get_children()[0].get_identifiers() ['self'] >>> table.get_children()[0].get_children()[1].get_identifiers() ['self', 'arg'] 

i can arguments of instance methods, while instance attributes missing.

therefore, i'm wondering if there way instance attributes. if not, have resort python module ast. can me this? thanks!

symtable executes code given it, not instantiate class a. follows interpreter through defining class a, methods, analysis of methods static - methods not executed, because class not instantiated. way understand - symtable shows information gathered during compilation of python code bytecode.

the ast module may here, won't in general case; because python dynamic, new attributes can added objects @ runtime, names being constructed user input - static analysis tools symtable , ast can't discover such methods. need run code proper hooks capture information.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

javascript - Clean way to programmatically use CSS transitions from JS? -

android - send complex objects as post php java -