12

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)

null
  • 548
  • 2
  • 6
  • 17

3 Answers3

14

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
>>>
5

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.

sagarchalise
  • 992
  • 8
  • 14
  • Alright, I'll give it a try tomorrow. (It's late here.) Thanks for the response mate. – null Apr 24 '15 at 03:37
  • `isinstance` or `type` doesn't work in many times, two object with same parent can be the same class. Using id comparison is more precise – TomSawyer Aug 29 '19 at 18:13
4

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.

Community
  • 1
  • 1
jonhurlock
  • 1,798
  • 1
  • 18
  • 28
  • 3
    I think that for this to work in Python 2.7, the classes need to inherit from `object` type because if you don't do so, then `type(instance_name)` will give you ``. Inheriting from `object` will make them behave more like Python 3 classes. Another (dubious) way to do it is `obj1.__class__.__name__ == obj2.__class__.__name__`. This works for Python 2 classes by default. – Shashank Apr 24 '15 at 02:42
  • 1
    This is true, when a class it is defined in python 2.x it should inherit from the object. So when defining your class make sure it looks as follows: `class MyCustomType(object):` not just `class MyCustomType:` – jonhurlock Apr 24 '15 at 02:51