Python Forum
How to determine how many variables are referring the same id in python?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to determine how many variables are referring the same id in python?
#1
consider the python code below
    divyansh = 22
    jhon = 22
    # below are id's
    id(divyansh)
    id(jhon)
    id(22)
    # all of the above ids will be same 
now i wish to find the names of the variables referring this particular id. |OR| The number of the variables reffing this particular id.

is there any way of doing this in python
Reply
#2
Look at sys.getrefcount and gc.get_referrers
Reply
#3
Why do want to do this?.
It also break over integer 256.
>>> divyansh = 2222
>>> jhon = 2222
>>> id(divyansh)
2362812091760
>>> id(jhon)
2362812091920
>>> id(2222)
2362812088752
Then it step out small integer optimization between -5 and 256.
Reply
#4
Quote:Why do want to do this?.
It also break over integer 256.
>>> divyansh = 2222
>>> jhon = 2222
>>> id(divyansh)
2362812091760
>>> id(jhon)
2362812091920
>>> id(2222)
2362812088752
Then it step out small integer optimization between -5 and 256.

actually i am doing this because i want to pass two variables to max() function and what ever is greater i want to have the name printed of that variable and the value of that variable,

ex- divyansh is the name of the person and 22 is the age
jhon is the name of the person and 21 is the age
so i want to have the name and the age of the person printed
so i did-->
divyansh=22
jhon =21
determine_the_name=max(divyansh,jhon)
# but here determine_the_name will have 22 not the variable name divyansh 
# so i taught that id of 22 will be same and if any how i can know the references of that id then it will lead to the variable name somehow

i know i can do this very easily through if-else ladder but i don't want to go that way,

and as we know this concept of referring works in decorators so i thought there can be a similar way to approach this
# in decorators we can get the real wrapper function name by using __name__ method onto the decorated method as
print(decorated_function.__name__) # will return the name of the actual function which is responsible the functionality of decorated_function 
Reply
#5
that's example, why your meaningful data (i.e. the name) should be kept out of variable name.
This would be nice example of using namedtuple

from collections import namedtuple
Person = namedtuple('Person', 'name age')

persons = [Person('John', 21), Person('Divyansh', 22)]
oldest = max(persons, key=lambda x: x.age)
print(oldest)
print('The oldest person is {name}, who is {age} years old.'.format(**oldest._asdict()))
# print(f'The oldest person is {oldest.name}, who is {oldest.age} years old.')

name, age = min(persons, key=lambda x: x.age)
print(f'The youngest person is {name}, who is {age} years old.')
Of course it could be done in a number of other ways... e.g.
data = {'var1': 21, 'var2':22}
key, value = max(data.items(), key=lambda x: x[1])
print(f'Max value {value} for key {key}')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
Variables should be thought of as, well, variables. Variables are a place to store a value, not the value itself. "name" is a good name for a variable, but "johnsmith" is not.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Determine number of all the immediately adjacent points in python zackk 1 601 Feb-06-2021, 09:23 AM
Last Post: zackk
  How to determine pen color from an image? robie972003 2 1,023 Mar-24-2019, 10:06 PM
Last Post: robie972003
  determine if an number is in a list Dbeah 7 1,740 Nov-06-2018, 12:11 PM
Last Post: buran
  determine if an number is in a list Dbeah 1 1,078 Nov-04-2018, 04:50 PM
Last Post: stullis
  how to determine if an object is a number Skaperen 6 2,017 Jul-11-2018, 08:18 PM
Last Post: Skaperen
  How Do I Determine indentation in AST? jimo 3 1,923 Jul-01-2018, 04:25 PM
Last Post: buran
  Determine whether a method was overridden porton 6 3,714 Nov-14-2016, 09:51 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020