Posts: 4,653
Threads: 1,496
Joined: Sep 2016
given an object that is in a list or tuple, is there a way to detect which type it is in?
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 1,583
Threads: 3
Joined: Mar 2020
Not directly. The object itself doesn't have knowledge of other objects that may refer to it. It may in fact be "in" tons of other objects, so it could simultaneously be in both lists and tuples. It might be "in" an object that is no longer referenced and will be garbage-collected away later.
Cpython internals have a reference count, but I'm not sure how you could access that. Even with the count, I don't think you can manipulate it to get the objects holding the reference.
You could potentially inspect all objects in your current scopes and examine them all of their contents looking for your target object, but I would look for other ways to solve the problem before attempting. How are you trying to use this information?
Posts: 379
Threads: 2
Joined: Jan 2021
May-15-2021, 01:15 AM
(This post was last modified: May-15-2021, 01:15 AM by BashBedlam.)
Here's one way to go about it using try and except and then parsing the error that is returned.
tester1 = ('this', 'and', 'that')
tester2 = ['this', 'and', 'that']
def list_or_tuple (test_element) :
try :
test_element ['dummy']
except TypeError as error:
return (error.__str__ ().split (' ') [0])
print (f'tester1 [0] is "{tester1 [0]}".')
print (f'tester1 is a {list_or_tuple (tester1)}.\n')
print (f'tester2 [2] is "{tester2 [2]}".')
print (f'tester2 is a {list_or_tuple (tester2)}.') Output: tester1 [0] is "this".
tester1 is a tuple.
tester2 [2] is "that".
tester2 is a list.
Posts: 1,583
Threads: 3
Joined: Mar 2020
That function reports if an object is a tuple or a list. You could that more directly via type() .
tester1 = ('this', 'and', 'that')
tester2 = ['this', 'and', 'that']
print (f'tester1 is a {type(tester1).__name__}.')
print (f'tester2 is a {type(tester2).__name__}.') Output: tester1 is a tuple.
tester2 is a list.
I interpreted the question as how do you tell what tester1 is when you don't have access to tester1 at all, just to one of the elements inside.
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
(May-15-2021, 12:09 AM)bowlofred Wrote: How are you trying to use this information? it is a function that is to output information about the object. it is a recursive function for references, so the caller will be dealing with the containing object, if there is one. so i just need pass that info in the call, such as another item in **kwargs.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Posts: 2,168
Threads: 35
Joined: Sep 2016
Could pass the parent type into the recursive function, something like the following:
def recursive_parent_obj(obj, parent_type):
if not obj or not isinstance(obj, (list, tuple)):
return
if isinstance(obj[0], (list, tuple)):
recursive_parent_obj(obj[0], type(obj[0]))
recursive_parent_obj(obj[1:], parent_type)
else:
print(f'first_obj: {obj[0]}, parent_type: {parent_type}')
recursive_parent_obj(obj[1:], parent_type)
obj = [1, 2, (11, [21, 22], 12, [31, 32], 13), 3, 4]
recursive_parent_obj(obj, type(obj)) Output: first_obj: 1, parent_type: <class 'list'>
first_obj: 2, parent_type: <class 'list'>
first_obj: 11, parent_type: <class 'tuple'>
first_obj: 21, parent_type: <class 'list'>
first_obj: 22, parent_type: <class 'list'>
first_obj: 12, parent_type: <class 'tuple'>
first_obj: 31, parent_type: <class 'list'>
first_obj: 32, parent_type: <class 'list'>
first_obj: 13, parent_type: <class 'tuple'>
first_obj: 3, parent_type: <class 'list'>
first_obj: 4, parent_type: <class 'list'>
Posts: 4,653
Threads: 1,496
Joined: Sep 2016
(May-15-2021, 01:15 AM)BashBedlam Wrote: Here's one way this is checking the whole list or tuple, not a contained item. i could just use type(foo).__name__
>>> a=['this','or','that']
>>> b=('this','or','that')
>>> type(a).__name__
'list'
>>> type(b).__name__
'tuple'
the real point is, the function that needed to know only had a reference to the item, not the container. but the was a called context where it mattered where the caller got the item from. the caller could have passed that along. that's the way i need to do this. the item might even be just a value like an int. the item could be in a tuple and in a list. or even a dict. or in some other kind of custom object.
Tradition is peer pressure from dead people
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
|