list extension ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stef Mientki

    list extension ?

    hello,

    I basically need a list with a few extra attributes,
    so I derived a new object from a list, and it works perfect.
    But I wonder why the newly derived list component is much more flexible ?

    # so here is the new list object
    class tGrid_List ( list ) :
    def __init__ ( self, value = [] ) :
    list.__init__ ( self, value )

    # and with this new list component, I can add new attributes on the fly

    a = tGrid_list ( [ 2, 3 ] )
    a.New_Attribute = 'some text'

    # I'm not allowed to this with the standard list

    a = [ 2, 3 ]
    a.New_Attribute = 'some text' <== ERROR

    Can someone explain this different behavior ?

    thanks,
    Stef Mientki
  • Scott David Daniels

    #2
    Re: list extension ?

    Stef Mientki wrote:
    .... (approximately, I PEP-8'ed it a bit) ...
    class tGrid_List(list ):
    def __init__(self, value=[]):
    list.__init__(s elf, value)
    # and with this new list component, I can add new attributes on the fly
    a = tGrid_list([2, 3])
    a.New_Attribute = 'some text'
    >
    # I'm not allowed to this with the standard list
    Can someone explain this different behavior ?
    Yes, you did not add a __slots__ member, so the object will have a
    __dict__ attribute. The __slots__ stuff is a storage optimization
    for frequently used data types (not worth it if you don't plan to
    have more than several thousand at any one time). Lists (for that
    matter most Python builtin types) get used a lot, so they have that
    built in.

    You can observe that even on the most trivial extensions:

    class AttributableInt (int): pass
    class AttributeFreeIn t(int): __slots__ = ()

    --Scott David Daniels
    Scott.Daniels@A cm.Org

    Comment

    • Bruno Desthuilliers

      #3
      Re: list extension ?

      Stef Mientki a écrit :
      hello,
      >
      I basically need a list with a few extra attributes,
      so I derived a new object from a list, and it works perfect.
      But I wonder why the newly derived list component is much more flexible ?
      >
      # so here is the new list object
      class tGrid_List ( list ) :
      pep08: class GridList(list):
      def __init__ ( self, value = [] ) :
      Gotcha : default argument values are eval'd only once. Also, it would
      make more sense IMHO to follow the parent's class initializer's behaviour:

      def __init__(self, *args)
      list.__init__ ( self, value )
      list.__init__(s elf, *args)
      # and with this new list component, I can add new attributes on the fly
      >
      a = tGrid_list ( [ 2, 3 ] )
      a = GridList(2, 3)

      or
      l = [2, 3]
      a = GridList(*l)
      a.New_Attribute = 'some text'
      >
      # I'm not allowed to this with the standard list
      >
      a = [ 2, 3 ]
      a.New_Attribute = 'some text' <== ERROR
      >
      Can someone explain this different behavior ?
      Most builtin types are optimized, and not having a __dict__ is part of
      this optimization.

      Comment

      • Stef Mientki

        #4
        Re: list extension ?

        thanks guys,
        >
        > def __init__ ( self, value = [] ) :
        >
        Gotcha : default argument values are eval'd only once. Also, it would
        make more sense IMHO to follow the parent's class initializer's
        behaviour:
        >
        def __init__(self, *args)
        >
        > list.__init__ ( self, value )
        >
        list.__init__(s elf, *args)
        >
        that's even better, except the call must be
        list.__init__ ( self, args )

        cheers,
        Stef

        Comment

        • Stef Mientki

          #5
          Re: list extension ?

          thanks guys,
          >
          > def __init__ ( self, value = [] ) :
          >
          Gotcha : default argument values are eval'd only once. Also, it would
          make more sense IMHO to follow the parent's class initializer's
          behaviour:
          >
          def __init__(self, *args)
          >
          > list.__init__ ( self, value )
          >
          list.__init__(s elf, *args)
          >
          that's even better, except the call must be
          list.__init__ ( self, args )

          cheers,
          Stef

          Comment

          • bruno.desthuilliers@gmail.com

            #6
            Re: list extension ?

            On 30 juin, 21:05, Stef Mientki <stef.mien...@g mail.comwrote:
            thanks guys,
            >
            def __init__ ( self, value = [] ) :
            >
            Gotcha : default argument values are eval'd only once. Also, it would
            make more sense IMHO to follow the parent's class initializer's
            behaviour:
            Ah hem.... Sorry, should have double-checked:
            def __init__(self, *args)
            >
            list.__init__ ( self, value )
            >
            list.__init__(s elf, *args)
            >
            that's even better, except the call must be
            list.__init__ ( self, args )
            Indeed. Which makes my whole comment about "following parent's class
            behaviour" totally off-tracks. Should have shut up here :(


            Comment

            Working...