M
Mr.Rech
Hi all,
I've read some thread about isinstance(), why it is considered harmful
and how you can achieve the same results using a coding style that
doesn't break polymorphism etc... Since I'm trying to improve my Python
knowledge, and I'm going to design a class hierarchy from scratch, I'd
like to have some feedback about this from those of you much more
experienced than me in OOP.
Suppose I'm writing a base class with an __eq__ special methods, using
isinstance() I would have wrote:
class foo(object):
...
def __eq__(self, other):
return isinstance(other, type(self)) and self.an_attribute ==
other.an__attribute
Now, avoiding isinstace() I've written the following code:
class foo(object):
...
def __eq__(self, other):
try:
return self.an_attribute == other.an_attribute
except AttributeError:
return False
Any better way to write this method? Any suggestion?
I've read some thread about isinstance(), why it is considered harmful
and how you can achieve the same results using a coding style that
doesn't break polymorphism etc... Since I'm trying to improve my Python
knowledge, and I'm going to design a class hierarchy from scratch, I'd
like to have some feedback about this from those of you much more
experienced than me in OOP.
Suppose I'm writing a base class with an __eq__ special methods, using
isinstance() I would have wrote:
class foo(object):
...
def __eq__(self, other):
return isinstance(other, type(self)) and self.an_attribute ==
other.an__attribute
Now, avoiding isinstace() I've written the following code:
class foo(object):
...
def __eq__(self, other):
try:
return self.an_attribute == other.an_attribute
except AttributeError:
return False
Any better way to write this method? Any suggestion?