Hi all
I have the following code.
class person(object):
def __init__(self, keys):
for item in keys:
setattr(self, item, None)
def __str__(self):
return str(self.__dict__)
def __eq__(self, other) :
return self.__dict__ == other.__dict__
Now I want to take this code and only do __eq__
on a specific set of attrs ("keys"). So I changed it to do this:
class person(object):
def __init__(self, keys):
self.valid_keys = keys
for item in keys:
setattr(self, item, None)
def __str__(self):
return dict([(i, getattr(self, i)) for i in self.valid_keys ])
def __eq__(self, other) :
assert isinstance(other, person)
self_vals = [ getattr(self, i) for i in self.valid_keys ]
other_vals = [ getattr(other, i) for i in self.valid_keys ]
return self_vals == other_vals
I have read the following two awesome posts (here and here) and my fundamental question is:
Is this the right approach or is there a better way to do this in python?
Obviously TMTOWTDI - but I'd like to keep and follow a standard pythonic approach. Thanks!!
Updates
I was asked why do I not fix the attrs in my class. This is a great question and here's why. The purpose of this is to take several dis-jointed employee records and build a complete picture of an employee. For example I get my data from ldap, lotus notes, unix passwd files, bugzilla data, etc. Each of those has uniq attrs and so I generalized them into a person. This gives me a quick consistent way to compare old records to new records. HTH. Thanks
** Updates Pt.2 **
Here is what I ended up with:
class personObj(object):
def __init__(self, keys):
self.__dict__ = dict.fromkeys(keys)
self.valid_keys = keys
def __str__(self):
return str([(i, getattr(self, i)) for i in self.valid_keys ])
def __eq__(self, other):
return isinstance(other, personObj) and all(getattr(self, i) == getattr(other, i) for i in self.valid_keys)
Thanks to both gents for reviewing!