I have in my program a class with a member function that should treat certain objects slightly differently than others. In order to do this, it needs to know which object was passed to it, because these objects would have peculiar data pointed to by their data members.But it seems that referencing the name of the invoking object is impractical and ill advised. I have also considered creating a new function for these exceptional objects, but then how would the next function down on the stack know to call it?I suppose I could create another data member as a sort of identifier and test for that when running my function; but that seems like a fairly hacky solution. What is the simplest way to do this? 
Making a member function treat two objects differently in Python
object oriented;python
There's a couple of ways you can do this:You can use isinstance to determine what the object's class is:if isinstance(foo, bar):  do_something()elif isinstance(foo, baz):  do_something_else()else:  default_behavior()However, this gets unwieldy quickly with a large number of possible classes, and isn't good OOP.The OOP way would be to use polymorphism - define a base class with a method that returns the data you need, and then create subclasses:class Polygon:  def area(self):    raise NotImplementedErrorclass Rectangle(Polygon):  def area(self):    return self.width * self.heightclass Circle(Polygon):  def area(self):    return math.pi * self.radius**2Then, in your other method:area = obj.area() # you don't have to worry about what type the object is anymore!A third, arguably more Pythonic way of going about this is to ask for forgiveness, not permission:try:  foo = obj.bar  do_something()except AttributeError: # if obj doesn't have a bar attribute, then it must have a baz attribute  foo = obj.baz  do_something_else()However, if you're using OOP, you should stick with the OOP way of doing things - let polymorphism do all the work.