[Chicago] Getting list of members of function in class

Kumar McMillan kumar.mcmillan at gmail.com
Wed Mar 7 23:59:44 CET 2007


On 3/7/07, Lukasz Szybalski <szybalski at gmail.com> wrote:
> Hello,
> I am trying to get a list of members of a function, namely:
>
> class A:
>     def dowork(self):
>         name='Lucas'
>         work=['aaa','bbb']
>         work1=[]
>         work2=[]
>
> How can i get a list of items that were created in dowork, namely:
> name,work,work1,work2?
> I could use dir(A) to get 'dowork', but that similar thing doesn't get
> me the next list, dir(A.dowork)

You'll have to get freaky with python internals.

>>> class A:
...    def dowork(self):
...        name='Lucas'
...        work=['aaa','bbb']
...        work1=[]
...        work2=[]
...
>>> A.dowork.im_func.func_code.co_names
('name', 'work', 'work1', 'work2')
>>>

the builtin module inspect [1] wraps up some of these things but I
don't see a method to get local var names, like above.

[1] http://docs.python.org/lib/module-inspect.html

PS.  there was a really cool blog post on Voidspace recently about
hacking code objects :
http://www.voidspace.org.uk/python/weblog/arch_d7_2006_11_18.shtml#e553


More information about the Chicago mailing list