'super' object has no attribute '__setitem__'

Discussion in 'Python' started by luvspython, Aug 19, 2011.

  1. luvspython

    luvspython Guest

    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
    luvspython, Aug 19, 2011
    #1
    1. Advertising

  2. luvspython <srehtvandy <at> gmail.com> writes:
    > 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.
    Benjamin Peterson, Aug 19, 2011
    #2
    1. Advertising

  3. luvspython

    Eric Snow Guest

    On Thu, Aug 18, 2011 at 7:44 PM, luvspython <> wrote:
    > 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

    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Eric Snow, Aug 19, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,737
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    708
    Bob Willan
    Nov 22, 2003
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    466
    Michele Simionato
    Jan 15, 2004
  4. johnny
    Replies:
    5
    Views:
    708
    johnny
    Dec 10, 2006
  5. jeremito
    Replies:
    12
    Views:
    540
    Bruno Desthuilliers
    Feb 7, 2007
Loading...

Share This Page