null
null

Reputation: 566

How to check if two instances are of the same class Python

So in my pygame game, I have created a list of objects to make things like updating them all and collision checking easier. So when I'm collision checking, I have to check if the current object is the same as the object we are collision checking with. Here is my current code:

def placeMeeting(self, object1, object2):

    # Define positioning variables
    object1Rect = pygame.Rect(object1.x, object1.y, object1.width, object1.height)

    # Weather or not they collided
    coll = False

    # Loop through all walls to check for possible collision
    for i in range(len(self.instances)):

        # First check if it's the right object
        if (self.instances[i] == object2):
            print "yep"
            object2Rect = pygame.Rect(self.instances[i].x, self.instances[i].y, self.instances[i].width, self.instances[i].height)

            # Check for collision with current wall -- Horizontal
            if (object1Rect.colliderect(object2Rect)):
                coll = True

    # Return the final collision result
    return coll

(All objects in the list/array are a child to the su)

Upvotes: 12

Views: 31934

Answers (3)

Amir Hossein Baghernezad
Amir Hossein Baghernezad

Reputation: 4085

Simple, yet powerful => type(a) is type(b)

>>> class A:
...     pass
...
>>> a = A()
>>> b = A()
>>> a is b
False
>>> a == b
False
>>> type(a)
<class '__main__.A'>
>>> type(b)
<class '__main__.A'>
>>> type(a) is type(b)
True
>>> type(a) == type(b)
True
>>>

Upvotes: 16

sagarchalise
sagarchalise

Reputation: 1020

Apart from type way in previous answer, I think you can use isinstance. https://docs.python.org/2/library/functions.html#isinstance

Operator is can be used for object checking such as a is b if a and b are same objects. Remember is checks only objects and not their values. Or, I havenot seen anyone do this, I guess id(obj1) == id(obj) would work as well when you need to check if two objects are same.

Upvotes: 4

jonhurlock
jonhurlock

Reputation: 1908

You can check to see the type of a variable using the type() function. So your code would read as below:

if(type(self.instances[i]) is MyCustomType):

This would check to see if instance[i] is of type MyCustomType. You can replace this with inbuilt types such as dict, list, int, str etc. or it can be custom types/objects you have declared.

It is important to note that it will only check the object type and not the object's values. So it will not see if two objects hold identical values.

It is also a bit tricky when we come to inheritance, so there are more examples in this answer Determine the type of an object?

Also take note from the comments to this answer, as if you are using Python 2.x and not inheriting from object when declaring custom Classes this solution may not work.


If you want to know if two instances of a class hold the same value you will have to implement the __eq__ function/method into the class definition. See SO answer Is there a way to check if two object contain the same values in each of their variables in python? for more details.

Upvotes: 4

Related Questions