-1

I need a little help with the following. If the answer is already out there and I didn't found it feel free to direct me to it :)

Explanation: I need to use the name of an object to call the object itself (easy) but also that very name as a string itself. So if I use the object 'some_object' inside a function I want to use that object and also to print the objects name - in this case 'some_object'.

some_object = 'xyz'


def some_function(object_name):
    print()  # should print the objects name
    print(object_name)


some_function(some_object)  # in this case it should print 'some_object'

# desired output: 
# some_object
# xyz

I really hope this is possible. Thank you very much for your time!

peter
  • 756
  • 5
  • 16
  • Does this answer your question? [Getting the name of a variable as a string](https://stackoverflow.com/questions/18425225/getting-the-name-of-a-variable-as-a-string) – jo3rn Feb 23 '21 at 15:39
  • 1
    Objects don't have names. You may find things that look sort of like they do what you're looking for, but they don't - they got upvoted by people who either didn't test them thoroughly or didn't test them at all. – user2357112 Feb 23 '21 at 15:39
  • Relying on the names that point to objects for anything is a very bad idea that cannot lead to anything good – rdas Feb 23 '21 at 15:41
  • @user2357112supportsMonica maybe I didn't expressed myself clear enough. Let me rephrase it. How can I print the argument inside the function as a string (meaning written characters themselves) – peter Feb 23 '21 at 15:44
  • Your function is only given an object. It sees `'xyz'` and that's it. It has no idea that the object was assigned to a variable previously, nor what name the variable had. Unless you give it a different object containing name information, you cannot really do what you are asking for. – Fricative Melon Feb 23 '21 at 15:49
  • @FricativeMelon thank you that helps me. Would the other way around be possible. Provide the function with the string 'some_object' and then let it find the object that is assigned to that variable? – peter Feb 23 '21 at 15:56
  • @peter That's what `dict`s are for: mapping a string (or any other hashable value) to another value. You may want to read https://nedbatchelder.com/text/names.html as a refresher for how Python names work. – chepner Feb 23 '21 at 16:00
  • @chepner thank you for the link. I will definitely check it out. However I want to avoid dicts at all cost. – peter Feb 23 '21 at 16:02
  • Any particular reason why? – chepner Feb 23 '21 at 16:03
  • 1
    @peter `globals()['some_object']` would give `'xyz'`. So your function can take the name of a global variable as a string, and get the value using that. Note that `globals()` is actually just a dictionary, containing all variables in global scope. – Fricative Melon Feb 23 '21 at 16:04
  • @FricativeMelon Yaaaay. This is exactly what I was looking for!!! I assume using `globals()['some_object']` is identical to `some_object` in terms of what happens computationally in the background. I am more than happy to mark this as a solution if you provide this as an answer. – peter Feb 23 '21 at 16:11
  • @chepner I have a lot of objects and wrapping all of them inside a dict would be too inefficient. – peter Feb 23 '21 at 16:14
  • A big thanks to everyone who spent time to help me! – peter Feb 23 '21 at 16:16
  • @FricativeMelon: That only works in the same module, and only for globals. Using that approach actively hinders maintainability of your code by forcing you into practices like overly-large "god" files and global variable abuse. – user2357112 Feb 23 '21 at 16:56

1 Answers1

1

The only method to me would be to use dictionaries. You refer the name of your key: then print this key with its value, and you should be good. I checked other info about this online, but it talks about classes and doing too compex stuff to do a simple thing like this.

When you create a variable and assign to this variable a value, the function dosen't care about the fact that the given value comes from a variable or not. I may be wrong but had to try helping, good luck.

E-Caps
  • 31
  • 4
  • 1
    Using this method would be very impractical for me but your suggestion is very appreciated. Maybe you have some ideas regarding my second questions in the comments. – peter Feb 23 '21 at 15:59
  • @peter print some_function.func_globals.keys()[some_function.func_globals.values().index(param)] – E-Caps Feb 23 '21 at 16:10