Given this class:
class vtp_datum(object):
def __init__(self,**kwargs):
for x in kwargs:
setattr(self,'__'+x,kwargs[x])
def __eq__(self,other):
return isinstance(other,vtp_datum) and (self.__ip_address == other.__ip_address and self.__mac_address == other.__mac_address)
def __hash__(self):
return hash( (self.__ip_address,self.__mac_address))
and this dictionary:
bleh={'ip_address': '1.2.3.4’, 'mac_address': '00:30:36:3B:30:39', 'pid': '1', 'pdesc': 'Dummy text', 'pname': 'Dummy P Name', 'description': 'Dummy Description'}
I'm not understanding why there would be attributes named _vtp_datum__<attribute> (as opposed to just __<attribute>) or why the hash function throws an error indicating attributes are missing.
>>> a=vtp_datum(**bleh)
>>> dir(a)
['__class__', '__delattr__', '__description', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__ip_address', '__le__', '__lt__', '__mac_address', '__module__', '__ne__', , '__new__', '__pdesc', '__pid', '__pname', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']
>>> a.__hash__()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "ctest.py", line 24, in __hash__
return hash( (self.__ip_address,self.__mac_address))
AttributeError: 'vtp_datum' object has no attribute '_vtp_datum__ip_address'
I tried it in a separate test module and the same thing happened (kinda). Maybe I haven't been paying attention but I never noticed class attributes that included the class name.
class blah:
def __init__(self,name):
self.__cheese = "craft"
setattr(self,'_name',name)
@property
def cheese(self):
return self.__cheese
@property
def name(self):
return self._name
>>> a=blah('yep')
>>> dir(a)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_blah__cheese', '_name', 'cheese', 'name']
I'd appreciate some help understanding what's going on here. Thanks