[Tutor] introspection

Alan Gauld alan.gauld at btinternet.com
Mon Apr 20 19:33:33 CEST 2015


On 20/04/15 17:55, Alex Kleider wrote:

>>>>> locals()
>> {'a': 3, 'c': 3, 'b': 6, '__builtins__': <module '__builtin__'
>> (built-in)>, '__package__': None, '__name__': '__main__', '__doc__':
>> None}

> Showing my desired use case might make my question more understandable:
> def debug(var_name):
>      if args["--debug"]:
>          print("Identifier <{}> is bound to: {}"
>              .format(var_name.__name__, repr(var_name)))
> I don't think the built in locals() can help me.

Why not? Provided you pass the required scope into your function:

def debug(var_name, scope):
     if args["--debug"]:
         print("Identifier <{}> is bound to: {}"
               .format(var_name, scope.get(var_name, 'no object'))

debug('a',locals()) # look in local vars


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list