Variable name introspection in Python?

In the general case, you cannot deduce the name from a value (there might be no name, there might be multiple ones, etc); when you call your hypothetical makedict(name) the value of name is what makedict receives, so (again, in the general case) it cannot discern what name (if any) the value came from. You could introspect your caller's namespaces to see if you're lucky enough to hit a special case where the value does let you infer the name (e.g. , you receive 23 and there's only one name throughout the namespaces of interest which happens to have a value of 23!), but that's clearly a fragile and iffy architecture. Plus, in your first example case, it's absolutely guaranteed that the special case will not occur -- the value in name is exactly the same as in either foo or baz so it's 100% certain that the name for that value will be hopelessly ambiguous You could take a completely different tack, such as calling makedict('name type', locals()) (passing locals() explicitly might be obviated with dark and deep introspection magic, but that's not the most solid choice in general) -- pass in the names (and namespaces, I suggest!

) and have makedict deduce the values which is obviously a much more solid proposition (since each name has exactly one value, but not viceversa).I.e. : def makedict(names, *namespaces): d = {} for n in names.split(): for ns in namespaces: if n in ns: dn = nsn break else: dn = None # or, raise an exception If you're keen on digging out the namespaces by introspection, rather than have them cleanly specified by the caller, look at inspect. Getouterframes but I suggest you reconsider The second issue you raise is quite different (though you could use inspect functions again to introspect the caller's name, or a function's own name -- what a peculiar idea!

). What's in common in the two cases is that you're using extremely powerful and dangerous machinery to do a job that could be done much more simply (easier to ensure correctness, easier to debug any problems, easier to test, etc, etc) -- far from having decorators be "overkill", they're far simpler and more explicit than the introspection you propose. If you have a zillion methods all of the form: def item_blah(self, item): dispatch("blah", item) the simplest way to create them might be: class Item(object): pass def _makedispcall(n): def item_whatever(self, item): dispatch(n, item) item_whatever.

__name__ = 'item_' + n return item_whatever for n in 'create delete blah but wait theres more'.split(): setattr(Item, 'item_' + n, _makedispcall(n)) Avoiding repetition is an excellent idea, but runtime introspection is not generally the best way to implement that idea, and Python offers many alternative ways to such implementation.

In the general case, you cannot deduce the name from a value (there might be no name, there might be multiple ones, etc); when you call your hypothetical makedict(name), the value of name is what makedict receives, so (again, in the general case) it cannot discern what name (if any) the value came from. You could introspect your caller's namespaces to see if you're lucky enough to hit a special case where the value does let you infer the name (e.g. , you receive 23, and there's only one name throughout the namespaces of interest which happens to have a value of 23!), but that's clearly a fragile and iffy architecture. Plus, in your first example case, it's absolutely guaranteed that the special case will not occur -- the value in name is exactly the same as in either foo or baz, so it's 100% certain that the name for that value will be hopelessly ambiguous.

You could take a completely different tack, such as calling makedict('name type', locals()) (passing locals() explicitly might be obviated with dark and deep introspection magic, but that's not the most solid choice in general) -- pass in the names (and namespaces, I suggest!) and have makedict deduce the values, which is obviously a much more solid proposition (since each name has exactly one value, but not viceversa). I.e. : def makedict(names, *namespaces): d = {} for n in names.split(): for ns in namespaces: if n in ns: dn = nsn break else: dn = None # or, raise an exception If you're keen on digging out the namespaces by introspection, rather than have them cleanly specified by the caller, look at inspect.

Getouterframes -- but I suggest you reconsider. The second issue you raise is quite different (though you could use inspect functions again to introspect the caller's name, or a function's own name -- what a peculiar idea! ).

What's in common in the two cases is that you're using extremely powerful and dangerous machinery to do a job that could be done much more simply (easier to ensure correctness, easier to debug any problems, easier to test, etc, etc) -- far from having decorators be "overkill", they're far simpler and more explicit than the introspection you propose. If you have a zillion methods all of the form: def item_blah(self, item): dispatch("blah", item) the simplest way to create them might be: class Item(object): pass def _makedispcall(n): def item_whatever(self, item): dispatch(n, item) item_whatever. __name__ = 'item_' + n return item_whatever for n in 'create delete blah but wait theres more'.split(): setattr(Item, 'item_' + n, _makedispcall(n)) Avoiding repetition is an excellent idea, but runtime introspection is not generally the best way to implement that idea, and Python offers many alternative ways to such implementation.

Thanks, this is very insightful. I expected that there'd be no simple solution, and ultimately agree that it's no worth the trouble. – AnC Dec 13 '09 at 9:20.

In general, you can't do this in Python, because Python has objects, not variables. If I have L1 = 1,2 L2 = L1 Then, L1 and L2 both refer to the same object. It has no single name.

Similarly: def f1(): pass f2 = f1 Now the function has no single name, and as such, you can't find out "the" name of the function. An object in Python can be referenced by many names - when the reference count of an object goes to zero, Python frees the memory for it.

Yeah, I had realized this, but figured there might be some way to figure it out nevertheless. Thanks! – AnC Dec 13 '09 at 9:21.

You can do what you want for functions: >>> def blagnorf(): pass >>> print blagnorf. __name__ blagnorf But not for variables, unfortunately. You could probably write a preprocessor for your python code to do it for you, though... Note that you can do this in Scheme/Lisp with the macro system they have there.

This only works outside the respective function though. Thanks anyway. – AnC Dec 13 '09 at 9:22 You can get the stack trace and get a reference to the current function being executed that way, from inside the function.

Or you can also do "blagnorf. __name__" inside the function. – Claudiu Dec 13 '09 at 16:27.

Whenever you get a hankering to make a gazillion simple methods whose internals only differ according to the method's name, you probably should reconsider your interface. Instead, you could do this: class Item(object): def item_action(self, action, item): dispatch(action, item) where action could be "create", "delete", etc.

I agree - unfortunately, I'm not in control of the interface here. – AnC Dec 13 '09 at 9:21.

In the general case, you cannot deduce the name from a value (there might be no name, there might be multiple ones, etc); when you call your hypothetical makedict(name), the value of name is what makedict receives, so (again, in the general case) it cannot discern what name (if any) the value came from. You could introspect your caller's namespaces to see if you're lucky enough to hit a special case where the value does let you infer the name (e.g. , you receive 23, and there's only one name throughout the namespaces of interest which happens to have a value of 23!), but that's clearly a fragile and iffy architecture. Plus, in your first example case, it's absolutely guaranteed that the special case will not occur -- the value in name is exactly the same as in either foo or baz, so it's 100% certain that the name for that value will be hopelessly ambiguous.

You could take a completely different tack, such as calling makedict('name type', locals()) (passing locals() explicitly might be obviated with dark and deep introspection magic, but that's not the most solid choice in general) -- pass in the names (and namespaces, I suggest!) and have makedict deduce the values, which is obviously a much more solid proposition (since each name has exactly one value, but not viceversa). If you're keen on digging out the namespaces by introspection, rather than have them cleanly specified by the caller, look at inspect. Getouterframes -- but I suggest you reconsider.

The second issue you raise is quite different (though you could use inspect functions again to introspect the caller's name, or a function's own name -- what a peculiar idea!). What's in common in the two cases is that you're using extremely powerful and dangerous machinery to do a job that could be done much more simply (easier to ensure correctness, easier to debug any problems, easier to test, etc, etc) -- far from having decorators be "overkill", they're far simpler and more explicit than the introspection you propose. Avoiding repetition is an excellent idea, but runtime introspection is not generally the best way to implement that idea, and Python offers many alternative ways to such implementation.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions