pythonclassobjectdictionarykey

How to check a dictionary for a class object value?


I have a dictionary, named

descendDict

And it contains 4 keys which are class objects, which have values that are both letters and other class objects.

Now what I'm trying to do is sort through the dictionary, and call out different actions if the value brought up in the dictionary is a class object, or if it is a letter:

for x in descendDict:
    print x, descendDict[x]
        for y in descendDict[x][0]:
            if y != (classObject?):
                #Action
            for x in descendDict:
                for z in descendDict[x][0]:
                    if z != (classObject?):
                        if y == z:
                            dist = 0
                        else:
                            dist = float(nodeDict[y]) + float(nodeDict[z])

In the if statements:

if... != (classObject?):

I am trying to determine whether the variable in the dictionary is, or is not a class object, but i just dont know how to do this.

Here is one the entries:

<__main__.Node instance at 0xb6738> ([<__main__.Node instance at 0xb6710>, 'A', <__main__.Node instance at 0xb6760>], '0.1')

I am sorting through it's first keys list, but i am trying to figure out if the values in the list are a class object, or a letter.


Solution

  • Not sure what you mean by "class object" since everything in Python is a first-class object. If you're trying to figure out if it's an instance of a specific class you can just use isinstance

    if isinstance(y, someClass):
        # do stuff