[Tutor] when I create an instance of a class that inherits from Python dictionary, my data disappears

tpc247 at gmail.com tpc247 at gmail.com
Sat Oct 7 03:44:01 CEST 2006


hi all, I would like to create a class that specializes Python dictionary.
I would like an instance of this class to store objects representing html
form data, and I would like to have an instance of this Data_Set class be
able to use the Python dictionary method pop to remove objects as I see
fit.  I defined the following:

class Data_Set(dict):
    def __init__(self, dict_of_datum_objects, required=None):
        self.keys = dict_of_datum_objects.keys
        self.values = dict_of_datum_objects.values
        self.required = required

For some reason, whenever I create an instance of this class with data, all
I get is an empty dictionary.  As soon as I redefine my class to no longer
inherit from Python dictionary, then I get expected behavior, but now I
don't have benefit of Python dictionary methods such as pop and has_key:

sdso = Data_Set({'full_name':'full_name_obj',
'address_line_1':'address_line_1_obj'})
>>> sdso
{}
>>> class Data_Set:
    def __init__(self, dict_of_datum, required=None):
    self.keys = dict_of_datum.keys
        self.values = dict_of_datum.values
        self.items = dict_of_datum.items
        self.required = required


>>> sdso = Data_Set({'full_name':'full_name_obj',
'address_line_1':'address_line_1_obj'})
>>> sdso
<__main__.Data_Set instance at 0x419690ac>
>>> sdso.keys()
['full_name', 'address_line_1']
>>> sdso.pop('full_name')

Traceback (most recent call last):
  File "<pyshell#311>", line 1, in -toplevel-
    sdso.pop('full_name')
AttributeError: Data_Set instance has no attribute 'pop'

Am I doing something wrong ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20061006/69644c9f/attachment.htm 


More information about the Tutor mailing list