Differences between revisions 3 and 7 (spanning 4 versions)
Revision 3 as of 2003-11-05 14:29:31
Size: 1273
Editor: xi
Comment: __slots__ has to be defined at class level; defining it in __init__ doesn't work
Revision 7 as of 2008-11-15 14:00:59
Size: 1328
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 13: Line 13:
class Msg( dict ): class Msg(dict):
Line 15: Line 16:
    def __init__( self, msg_type, kv_dict = {} ):
        dict.__init__( self )
        self[ "msg-type" ] = msg_type
        self.update( kv_dict )
    def Type( self ):
        return self[ "msg-type" ]
    def __getitem__( self, k ):
        return self.get( k, None )
    def __delitem__( self, k
):
        if self.has_key( k ):
            dict.__delitem__( self, k )
    def __str__( self
):
        pp = pprint.pformat( dict(self) )
        return "%s: %s" % (self.Type(), pp )

    def __init__(
self, msg_type, kv_dict = {}):
        dict.__init__(self)
        self["msg-type"] = msg_type
        self.update(kv_dict)

    def Type(self):
        return self["msg-type"]

    def __getitem__(self, k
):
        return self.get(k, None)

    def __delitem__(self, k
):
        if self.has_key(k):
            dict.__delitem__(self, k)

    def __str__(self
):
        pp = pprint.pformat(dict(self))
        return "%s: %s" % (self.Type(), pp)
Line 35: Line 41:
["Python-2.2"], SubclassingBuiltInTypes, UsingSlots [[Python-2.2]], SubclassingBuiltInTypes, UsingSlots
Line 39: Line 45:
  * Is this bad Python-2.2 code? Make improvements..! I ''do'' think it's worth showing how to use slots in the context of subclassing dict; In many cases, I think, people would want to do it. I ''do'' wonder if slots should be specified before or after the initializer- something to put on the UsingSlots page. -- LionKimbro [[DateTime(2003-09-07T17:07:24Z)]]   * Is this bad Python-2.2 code? Make improvements..! I ''do'' think it's worth showing how to use slots in the context of subclassing dict; In many cases, I think, people would want to do it. I ''do'' wonder if slots should be specified before or after the initializer- something to put on the UsingSlots page. -- LionKimbro <<DateTime(2003-09-07T17:07:24Z)>>
  [[lwickjr
]]: Before, I think, collected with the other declaritives.

Subclassing Dictionaries

The process differs by Python version.

Python-2.2

Derive from dict.

ex:

   1 class Msg(dict):
   2 
   3     __slots__ = [] # no attributes
   4 
   5     def __init__(self, msg_type, kv_dict = {}):
   6         dict.__init__(self)
   7         self["msg-type"] = msg_type
   8         self.update(kv_dict)
   9 
  10     def Type(self):
  11         return self["msg-type"]
  12 
  13     def __getitem__(self, k):
  14         return self.get(k, None)
  15 
  16     def __delitem__(self, k):
  17         if self.has_key(k):
  18             dict.__delitem__(self, k)
  19 
  20     def __str__(self):
  21         pp = pprint.pformat(dict(self))
  22         return "%s:  %s" % (self.Type(), pp)

The __slots__ line indicates that Msg has no attributes of its own, preserving memory; see UsingSlots.

See Also

Python-2.2, SubclassingBuiltInTypes, UsingSlots

Questions

  • Is this bad Python-2.2 code? Make improvements..! I do think it's worth showing how to use slots in the context of subclassing dict; In many cases, I think, people would want to do it. I do wonder if slots should be specified before or after the initializer- something to put on the UsingSlots page. -- LionKimbro 2003-09-07 17:07:24 lwickjr: Before, I think, collected with the other declaritives.

SubclassingDictionaries (last edited 2008-11-15 14:00:59 by localhost)

Unable to edit the page? See the FrontPage for instructions.