'super' object has no attribute '__setitem__'

L

luvspython

I'm using Python 2.7 and the code below fails at the 'super' statement
in the __setitem__ function in the HistoryKeeper class. The error is:
'super' object has no attribute '_setitem__'

Can anyone please tell me why and how to fix it? (I've googled
endlessly and I don't see the problem.)

[The code will seem silly as it is, because it's pared down to show
the example. The goal is that multiple classes, like the Vehicle
class below, will inherit HistoryKeeper. History keeper overloads
__setitem__ and will eventually keep a running history every time an
attribute of any of the inheriting classes is changed.]

Thanks in advance ....


class HistoryKeeper(object):
def __init__(self, args):
for arg, value in args.items():
if arg != 'self':
self.__setitem__(arg, value)

def __setitem__(self, item, value):
super(HistoryKeeper, self).__setitem__(item, value)


class Vehicle(HistoryKeeper):
def __init__(self, tag, make, model):
args = locals()
super(Vehicle, self).__init__(args)


if __name__ == "__main__":
car = Vehicle('TAG123', 'FORD', 'Model A')
print car.make
 
B

Benjamin Peterson

luvspython said:
def __setitem__(self, item, value):
super(HistoryKeeper, self).__setitem__(item, value)

object has no __setitem__. Are you looking for __setattr__?
class Vehicle(HistoryKeeper):
def __init__(self, tag, make, model):
args = locals()

This is hideous by the way.
 
E

Eric Snow

I'm using Python 2.7 and the code below fails at the 'super' statement
in the __setitem__ function in the HistoryKeeper class.  The error is:
  'super' object has no attribute '_setitem__'

Can anyone please tell me why and how to fix it?   (I've googled
endlessly and I don't see the problem.)

[The code will seem silly as it is, because it's pared down to show
the example.  The goal is that multiple classes, like the Vehicle
class below, will inherit HistoryKeeper.  History keeper overloads
__setitem__ and will eventually keep a running history every time an
attribute of any of the inheriting classes is changed.]

Thanks in advance ....


class HistoryKeeper(object):
   def __init__(self, args):
       for arg, value in args.items():
           if arg != 'self':
               self.__setitem__(arg, value)

   def __setitem__(self, item, value):
       super(HistoryKeeper, self).__setitem__(item, value)


class Vehicle(HistoryKeeper):
   def __init__(self, tag, make, model):
       args = locals()
       super(Vehicle, self).__init__(args)


if __name__ == "__main__":
   car = Vehicle('TAG123', 'FORD', 'Model A')
   print car.make

Did you mean to use __setattr__ instead? object, the base class of
HistoryKeeper, does not have a __setitem__ method, hence the
AttributeError. super() is a proxy for the next class in the MRO,
typically the base class of your class.

Keep in mind that <obj.tag = "TAG123"> is equivalent to
<obj.__setattr__("tag", "TAG123")>. However, <obj["tag"] = "TAG123">
is equivalent to <obj.__setitem__("tag", "TAG123")>.

see:

http://docs.python.org/reference/datamodel.html#object.__setattr__
http://docs.python.org/reference/datamodel.html#object.__setitem__
http://docs.python.org/library/functions.html#super

-eric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top