How to determine how many variables are referring the same id in python? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to determine how many variables are referring the same id in python? (/thread-28809.html) |
How to determine how many variables are referring the same id in python? - divyansh - Aug-04-2020 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 samenow 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 RE: How to determine how many variables are referring the same id in python? - deanhystad - Aug-04-2020 Look at sys.getrefcount and gc.get_referrers RE: How to determine how many variables are referring the same id in python? - snippsat - Aug-04-2020 Why do want to do this?. It also break over integer 256. >>> divyansh = 2222 >>> jhon = 2222 >>> id(divyansh) 2362812091760 >>> id(jhon) 2362812091920 >>> id(2222) 2362812088752Then it step out small integer optimization between -5 and 256. RE: How to determine how many variables are referring the same id in python? - divyansh - Aug-04-2020 Quote:Why do want to do this?. 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 RE: How to determine how many variables are referring the same id in python? - buran - Aug-04-2020 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}') RE: How to determine how many variables are referring the same id in python? - deanhystad - Aug-04-2020 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. |