Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to extract variable name of a dictionary entry?

I'm wondering how I would go about finding the variable name of a dictionary element:

For example:

    >>>dict1={}
    >>>dict2={}

    >>>dict1['0001']='0002'
    >>>dict2['nth_dict_item']=dict1
    >>>print dict2
    {'nth_dict_item': {'0001': '0002'}}
    >>>print dict2['nth_dict_item']
    {'001': '002'}

How can I go about making it tell me that dict2['nth_dict_item'] is or is referencing "dict1" ? I want the name of the data structure it's referencing and not the data itself.

If I compare the output of id(dict1) to id(dict2['nth_dict_item']) I can find they are the same.

But how can I turn that id back into a variable name? Is there a more direct/cleaner method of getting the information that I want to know?

I'm sure I'm just overlooking a function that would make my life easy but I'm pretty new to Python :)

Any help is appreciated, thanks!

Related

  • python, can i print original var name?
  • How can you print a variable name in python?

Update: Here's why I wanted this to work:

I'm trying to make an app that uses a dictionary kinda like a database. I wanted the functionality of this psuedocode to function:

dict_1={}
dict_2={}
dict_3={}

dict_1["FooBar1.avi"]=[movie_duration,movie_type,comments]
dict_2["FooBar2.avi"]=[movie_duration,movie_type,comments]
dict_3["FooBar3.avi"]=[movie_duration,movie_type,comments]

dict_database[SomeUniqueIdentifier1]=dict_1
dict_database[SomeUniqueIdentifier2]=dict_2
dict_database[SomeUniqueIdentifier3]=dict_3

SomeUniqueIdentifier# would be a unique value that I'm using as a database key/unqiueID to look up entries.

I want to be able to update the "comments" field of FooBar1.avi by:

WhichDict= dict_database[SomeUniqueIdentifier1]
WhichDict[WhichDict.keys()[0]][2]='newcomment'

instead of having to do:

dict_database['SomeUniqueIdentifier1'][dict_database['SomeUniqueIdentifier1'].keys()[0]][2]='newcomment'

Thanks everyone. I now understand I was misunderstanding a LOT of basics (total brain fart). Will go back and fix the design. Thanks to you all!.

like image 330
Brandon K Avatar asked Jun 30 '26 23:06

Brandon K


1 Answers

A variable name is just a name--it has no real meaning as far as the program is concerned, except as a convenience to the programmer (this isn't quite true in Python, but bear with me). As far as the Python interpreter is concerned, the name dict1 is just the programmer's way of telling Python to look at memory address 0x1234.

Furthermore, the very idea of turning a memory address (0x1234) back into a variable name (dict1) doesn't make sense, especially considering that more than one variable name can reference the same object:

dict1 = {...}
dictCopy = dict1

Now dictCopy is just as much the name of the dictionary as dict1 is--they both reference the same thing. If the function you're looking for existed, which name should Python choose?

like image 187
zenazn Avatar answered Jul 03 '26 13:07

zenazn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!