Python Forum

Full Version: in a list or tuple
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
given an object that is in a list or tuple, is there a way to detect which type it is in?
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?
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.
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.
(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.
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'>
(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.