Re: [Tutor] introspection

2015-05-08 Thread Dave Angel
On 05/08/2015 02:26 AM, Alex Kleider wrote: On 2015-05-07 20:45, Dave Angel wrote: You also only showed it working on module globals. (For code at top-level, locals() returns the same as globals() ) You could also try it inside functions, where locals() really makes sense as a name. And you

Re: [Tutor] introspection

2015-05-07 Thread Alex Kleider
On 2015-05-07 20:45, Dave Angel wrote: You also only showed it working on module globals. (For code at top-level, locals() returns the same as globals() ) You could also try it inside functions, where locals() really makes sense as a name. And you could try it in a nested function where there

Re: [Tutor] introspection

2015-05-07 Thread Dave Angel
On 05/07/2015 11:23 PM, Alex Kleider wrote: On 2015-05-07 19:10, Dave Angel wrote: def get_name(localmap, item): """As suggested. Returns 'a' name, not necessarily 'the' name.""" for name in localmap: if localmap[name] == item: This is not likely to be what was intende

Re: [Tutor] introspection

2015-05-07 Thread Alex Kleider
On 2015-05-07 19:10, Dave Angel wrote: def get_name(localmap, item): """As suggested. Returns 'a' name, not necessarily 'the' name.""" for name in localmap: if localmap[name] == item: This is not likely to be what was intended. You want if localmap[name] is

Re: [Tutor] introspection

2015-05-07 Thread Dave Angel
On 05/07/2015 09:50 PM, Alex Kleider wrote: On 2015-04-21 16:48, Cameron Simpson wrote: But it would not be schizophrenic to write a function that returned a name arbitrarily, by inspecting locals(). It depends whether you only need a name, or if you need "the" name. Write yourself a "find_nam

Re: [Tutor] introspection

2015-05-07 Thread Alex Kleider
On 2015-04-21 16:48, Cameron Simpson wrote: But it would not be schizophrenic to write a function that returned a name arbitrarily, by inspecting locals(). It depends whether you only need a name, or if you need "the" name. Write yourself a "find_name_from_locals(local_map, value)" function tha

Re: [Tutor] introspection

2015-04-21 Thread Alex Kleider
On 2015-04-21 16:38, Ben Finney wrote: That hope is understandable. Your "understanding" is appreciated. It is also easy to be confused So true, but with the help of "Python Tutors" things are being rectified! about why such a feature doesn't exist; So why not arbitrary objects?

Re: [Tutor] introspection

2015-04-21 Thread Alex Kleider
On 2015-04-21 16:48, Cameron Simpson wrote: But it would not be schizophrenic to write a function that returned a name arbitrarily, by inspecting locals(). It depends whether you only need a name, or if you need "the" name. In my use case there'd probably be only one name for the given object

Re: [Tutor] introspection

2015-04-21 Thread Cameron Simpson
On 21Apr2015 09:59, Alex Kleider wrote: On 2015-04-20 22:21, Danny Yoo wrote: What's supposed to happen in this situation? ## class Person(object): def __init__(self): pass j = Person() john = j jack = j ## Wha

Re: [Tutor] introspection

2015-04-21 Thread Ben Finney
Alex Kleider writes: > I was hoping that it would be possible to create a function > that would do the following: > > def my_name(some_object): > return some_object.__name__ That hope is understandable. It is also easy to be confused about why such a feature doesn't exist; after all, it works

Re: [Tutor] introspection

2015-04-21 Thread Danny Yoo
> But I see what I think you and others have been trying to explain to me: > that the expression some_object.__name__, if it existed, would indeed be > schizophrenic since it would be an attribute of the object, not the name(s) > to which it is bound. The hypothetical feature might also, if desi

Re: [Tutor] introspection

2015-04-21 Thread Alex Kleider
On 2015-04-20 22:21, Danny Yoo wrote: What's supposed to happen in this situation? ## class Person(object): def __init__(self): pass j = Person() john = j jack = j ## What single name should we get back from t

Re: [Tutor] introspection

2015-04-21 Thread Dave Angel
On 04/21/2015 01:21 AM, Danny Yoo wrote: What's supposed to happen in this situation? ## class Person(object): def __init__(self): pass j = Person() john = j jack = j ## What single name should we get back fr

Re: [Tutor] introspection

2015-04-20 Thread Danny Yoo
What's supposed to happen in this situation? ## class Person(object): def __init__(self): pass j = Person() john = j jack = j ## What single name should we get back from the single Person object here? "j", "joh

Re: [Tutor] introspection

2015-04-20 Thread Alex Kleider
On 2015-04-20 13:24, Ben Finney wrote: Alex Kleider writes: Does python provide the introspective ability to retrieve the name to which an object is bound? Objects aren't bound to names. So, no. The binding from a reference (a name is a reference) to objects is one-way. See this excellent

Re: [Tutor] introspection

2015-04-20 Thread Ben Finney
Alex Kleider writes: > Does python provide the introspective ability to retrieve the name to > which an object is bound? Objects aren't bound to names. So, no. The binding from a reference (a name is a reference) to objects is one-way. See this excellent presentation from PyCon US 2015 by Ned

Re: [Tutor] introspection

2015-04-20 Thread Peter Otten
Alex Kleider wrote: > On 2015-04-20 09:15, Joel Goldstick wrote: >> On Mon, Apr 20, 2015 at 11:24 AM, Alex Kleider >> wrote: >>> Does python provide the introspective ability to retrieve the name to >>> which >>> an object is bound? >>> >>> For example: >>> $ python3 >>> Python 3.4.0 (default, A

Re: [Tutor] introspection

2015-04-20 Thread Alan Gauld
On 20/04/15 17:55, Alex Kleider wrote: locals() {'a': 3, 'c': 3, 'b': 6, '__builtins__': , '__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("Ident

Re: [Tutor] introspection

2015-04-20 Thread Steven D'Aprano
On Mon, Apr 20, 2015 at 08:24:42AM -0700, Alex Kleider wrote: > Does python provide the introspective ability to retrieve the name to > which an object is bound? Short answer: no. Slightly longer answer: it *may* be possible if you dig deep into the debugger API that you might find something wh

Re: [Tutor] introspection

2015-04-20 Thread Alex Kleider
On 2015-04-20 09:15, Joel Goldstick wrote: On Mon, Apr 20, 2015 at 11:24 AM, Alex Kleider wrote: Does python provide the introspective ability to retrieve the name to which an object is bound? For example: $ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:18) [GCC 4.8.2] on linux Type "help

Re: [Tutor] introspection

2015-04-20 Thread Joel Goldstick
On Mon, Apr 20, 2015 at 11:24 AM, Alex Kleider wrote: > Does python provide the introspective ability to retrieve the name to which > an object is bound? > > For example: > $ python3 > Python 3.4.0 (default, Apr 11 2014, 13:05:18) > [GCC 4.8.2] on linux > Type "help", "copyright", "credits" or "li

[Tutor] introspection

2015-04-20 Thread Alex Kleider
Does python provide the introspective ability to retrieve the name to which an object is bound? For example: $ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:18) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. a = 69 print("Identifier <{}> is bound

Re: [Tutor] Introspection (modules and functions)

2005-11-23 Thread Kent Johnson
Negroup - wrote: > Hi. > > My application hosts a module A that contains only a (variable) number > of functions. In another part of this application I need to know which > functions are defined inside module A. Initially I thought to use > __dict__, but along with the functions of module A are li

Re: [Tutor] Introspection (modules and functions)

2005-11-23 Thread Ewald Ertl
Hi! I quick solution for a name module could be: >>> import os >>> for d in os.__dict__: ... a="os." + d ... if callable( eval(a) ): ... print "Callable %s" % ( eval(a)) but there should also be a recipe on activestate for that problem. I think I've red something in the Pytho

Re: [Tutor] Introspection (modules and functions)

2005-11-23 Thread Frank Moore
Negroup - wrote: >Hi. > >My application hosts a module A that contains only a (variable) number >of functions. In another part of this application I need to know which >functions are defined inside module A. Initially I thought to use >__dict__, but along with the functions of module A are listed

[Tutor] Introspection (modules and functions)

2005-11-23 Thread Negroup -
Hi. My application hosts a module A that contains only a (variable) number of functions. In another part of this application I need to know which functions are defined inside module A. Initially I thought to use __dict__, but along with the functions of module A are listed all the builtins too. H