subclassing list and adding other variables ?

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

    subclassing list and adding other variables ?

    hello,

    i wonder if this possible to subclass a list or a tuple and add more
    attributes ? also does someone have a link to how well define is own
    iterable object ?

    what i was expecting was something like :
    [color=blue][color=green][color=darkred]
    >>> t = Test('anAttribu teValue', ['el1', 'el2'])
    >>> t.anAttribute[/color][/color][/color]
    'anAttributeVal ue'[color=blue][color=green][color=darkred]
    >>> for x in t:[/color][/color][/color]
    print x
    el1
    el2

    and below are some real unsuccessful tests :
    [color=blue][color=green][color=darkred]
    >>> class Test(tuple):[/color][/color][/color]
    def __init__(self, a, alist):
    self.a = a
    self = tuple(alist)

    [color=blue][color=green][color=darkred]
    >>> t = Test(1, (1,2,3))[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#486>" , line 1, in -toplevel-
    t = Test(1, (1,2,3))
    TypeError: tuple() takes at most 1 argument (2 given)[color=blue][color=green][color=darkred]
    >>> class Test(tuple):[/color][/color][/color]
    def __init__(self, (a, alist)):
    self.a = a
    self = tuple(alist)

    [color=blue][color=green][color=darkred]
    >>> t = Test((1, (1,2,3)))
    >>> t.a[/color][/color][/color]
    1[color=blue][color=green][color=darkred]
    >>> t.a = 2
    >>> t.a[/color][/color][/color]
    2[color=blue][color=green][color=darkred]
    >>> len(t)[/color][/color][/color]
    2[color=blue][color=green][color=darkred]
    >>> t[/color][/color][/color]
    (1, (1, 2, 3))[color=blue][color=green][color=darkred]
    >>> t[2][/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#495>" , line 1, in -toplevel-
    t[2]
    IndexError: tuple index out of range[color=blue][color=green][color=darkred]
    >>> t[1][/color][/color][/color]
    (1, 2, 3)[color=blue][color=green][color=darkred]
    >>> class Test(tuple):[/color][/color][/color]
    def __init__(self, (a, alist)):
    self.__init__(t uple(alist))
    self.a = a

    [color=blue][color=green][color=darkred]
    >>> t = Test((1, (1,2,3)))[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#500>" , line 1, in -toplevel-
    t = Test((1, (1,2,3)))
    File "<pyshell#499>" , line 3, in __init__
    self.__init__(t uple(alist))
    File "<pyshell#499>" , line 2, in __init__
    def __init__(self, (a, alist)):
    ValueError: unpack tuple of wrong size[color=blue][color=green][color=darkred]
    >>> class Test(tuple):[/color][/color][/color]
    def __init__(self, alist, a = None):
    self.__init__(t uple(alist))
    self.a = a

    [color=blue][color=green][color=darkred]
    >>> t = Test((1,2,3), 4)[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#503>" , line 1, in -toplevel-
    t = Test((1,2,3), 4)
    TypeError: tuple() takes at most 1 argument (2 given)[color=blue][color=green][color=darkred]
    >>> class Test(tuple):[/color][/color][/color]
    def __init__(self, (alist, a = None)):
    self.__init__(t uple(alist))
    self.a = a

    SyntaxError: invalid syntax[color=blue][color=green][color=darkred]
    >>>
    >>> class Test(list):[/color][/color][/color]
    def __init__(self, (a, alist)):
    self.a = a
    self = tuple(alist)

    [color=blue][color=green][color=darkred]
    >>> t = Test((4, (1,2,3)))
    >>> t[/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>> class Test(list):[/color][/color][/color]
    def __init__(self, (a, alist)):
    self.a = a
    self = alist

    [color=blue][color=green][color=darkred]
    >>> t = Test((4, (1,2,3)))
    >>> t[/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>> t.a[/color][/color][/color]
    4[color=blue][color=green][color=darkred]
    >>> len(t)[/color][/color][/color]
    0[color=blue][color=green][color=darkred]
    >>> class Test(list):[/color][/color][/color]
    def __init__(self, a, alist):
    self.a = a
    self = alist

    [color=blue][color=green][color=darkred]
    >>> t = Test(4, (1,2,3))
    >>> t[/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>> t.a[/color][/color][/color]
    4[color=blue][color=green][color=darkred]
    >>> class Test(list):[/color][/color][/color]
    def __init__(self, a, alist):
    self.a = a
    self.__init__(a list)

    [color=blue][color=green][color=darkred]
    >>> t = Test(4, (1,2,3))[/color][/color][/color]

    Traceback (most recent call last):
    File "<pyshell#523>" , line 1, in -toplevel-
    t = Test(4, (1,2,3))
    File "<pyshell#522>" , line 4, in __init__
    self.__init__(a list)
    TypeError: __init__() takes exactly 3 arguments (2 given)


  • GrelEns

    #2
    Re: subclassing list and adding other variables ?


    "GrelEns" <grelens@NOSPAM yahoo.NOTNEEDED fr> a écrit dans le message de news:
    4050d0df$0$289$ 626a14ce@news.f ree.fr...[color=blue]
    > hello,
    >
    > i wonder if this possible to subclass a list or a tuple and add more
    > attributes ? also does someone have a link to how well define is own
    > iterable object ?
    >
    > what i was expecting was something like :
    >[color=green][color=darkred]
    > >>> t = Test('anAttribu teValue', ['el1', 'el2'])
    > >>> t.anAttribute[/color][/color]
    > 'anAttributeVal ue'[color=green][color=darkred]
    > >>> for x in t:[/color][/color]
    > print x
    > el1
    > el2[/color]

    i reply to myself, but still have questions ;)

    so this one works :
    [color=blue][color=green][color=darkred]
    >>> class Test(list):[/color][/color][/color]
    def __init__(self, a):
    self.a = a
    def load(self, alist):
    self.extend(ali st)

    [color=blue][color=green][color=darkred]
    >>> t = Test(4)
    >>> t[/color][/color][/color]
    [][color=blue][color=green][color=darkred]
    >>> t.load((1,2,3))
    >>> t[/color][/color][/color]
    [1, 2, 3]

    what do you think of such a design ? is there some underlying flaw that i
    should be aware of ?

    (and about the tuple stuffs not working in my previous examples, i suppose
    this is because a tuple is immutable and thus should not be modified after
    creation)

    thx


    Comment

    • Gerrit

      #3
      Re: subclassing list and adding other variables ?

      GrelEns wrote:[color=blue]
      > i wonder if this possible to subclass a list or a tuple and add more
      > attributes ? also does someone have a link to how well define is own
      > iterable object ?[/color]

      For subclassing builtin types, read:



      Gerrit.

      --
      Weather in Twenthe, Netherlands 11/03 20:25 UTC:
      2.0°C wind 2.7 m/s E (57 m above NAP)
      --
      Asperger's Syndrome - a personal approach:


      Comment

      • Peter Otten

        #4
        Re: subclassing list and adding other variables ?

        GrelEns wrote:
        [color=blue]
        > i wonder if this possible to subclass a list or a tuple and add more
        > attributes ? also does someone have a link to how well define is own
        > iterable object ?[/color]

        [You tried hard]

        With lists it is the standard procedure of overriding __init__() and calling
        the baseclass method:
        [color=blue][color=green][color=darkred]
        >>> class List(list):[/color][/color][/color]
        .... def __init__(self, iterable, a):
        .... list.__init__(s elf, iterable)
        .... self.a = a
        ....[color=blue][color=green][color=darkred]
        >>> a = List((1,2,3), "abc")
        >>> a[/color][/color][/color]
        [1, 2, 3][color=blue][color=green][color=darkred]
        >>> a.a[/color][/color][/color]
        'abc'

        Tuples are immutable and thus changes in __init__() will not affect the
        tuple items. They are set in the __new__() method instead.
        [color=blue][color=green][color=darkred]
        >>> class Tuple(tuple):[/color][/color][/color]
        .... def __new__(cls, *args):
        .... return tuple.__new__(c ls, args[0])
        .... def __init__(self, seq, a):
        .... self.a = a
        ....[color=blue][color=green][color=darkred]
        >>> b = Tuple((1,2,3), "abc")
        >>> b[/color][/color][/color]
        (1, 2, 3)[color=blue][color=green][color=darkred]
        >>> b.a[/color][/color][/color]
        'abc'

        For a minimal iterable class, let __iter__() return self and next()
        calculate the next value or raise a StopIteration exception when there are
        no more values:
        [color=blue][color=green][color=darkred]
        >>> class Iterable:[/color][/color][/color]
        .... def __init__(self, start, maxval):
        .... self.value = start
        .... self.maxval = maxval
        .... def __iter__(self): return self
        .... def next(self):
        .... if self.value > self.maxval:
        .... raise StopIteration
        .... result = self.value
        .... self.value *= -2
        .... return result
        ....[color=blue][color=green][color=darkred]
        >>> for n in Iterable(1, 500):[/color][/color][/color]
        .... print n,
        ....
        1 -2 4 -8 16 -32 64 -128 256 -512

        (I've got a hunch that Iterator would have been a more appropriate name, but
        you may judge on your own, see
        http://www.python.org/doc/current/tut/node17.html)

        Peter

        Comment

        • GrelEns

          #5
          Re: subclassing list and adding other variables ?


          "Peter Otten" <__peter__@web. de> a écrit dans le message de news:
          c2qr32$j1n$01$1 @news.t-online.com...[color=blue]
          > GrelEns wrote:
          >[color=green]
          > > i wonder if this possible to subclass a list or a tuple and add more
          > > attributes ? also does someone have a link to how well define is own
          > > iterable object ?[/color]
          >
          > With lists it is the standard procedure of overriding __init__() and[/color]
          calling[color=blue]
          > the baseclass method:
          >[color=green][color=darkred]
          > >>> class List(list):[/color][/color]
          > ... def __init__(self, iterable, a):
          > ... list.__init__(s elf, iterable)
          > ... self.a = a
          > ...[color=green][color=darkred]
          > >>> a = List((1,2,3), "abc")
          > >>> a[/color][/color]
          > [1, 2, 3][color=green][color=darkred]
          > >>> a.a[/color][/color]
          > 'abc'[/color]

          thanks, so i supposed that this is a correct way to do :
          [color=blue][color=green][color=darkred]
          >>> class List(list):[/color][/color][/color]
          def __init__(self, v = None):
          self.v = v
          def load(self, values):
          list.extend(sel f, values)
          def gets(self):
          return list(self)
          [color=blue][color=green][color=darkred]
          >>> l = List(5)
          >>> l.load([1,2,3])
          >>> l[/color][/color][/color]
          [1, 2, 3][color=blue][color=green][color=darkred]
          >>> l.gets()[/color][/color][/color]
          [1, 2, 3][color=blue][color=green][color=darkred]
          >>> type(l.gets())[/color][/color][/color]
          <type 'list'>[color=blue][color=green][color=darkred]
          >>> type(l)[/color][/color][/color]
          <class '__main__.List' >

          BTW while i think i get it with the list.extend(sel f, values) which is i
          suppose

          super_class.met hod_from_super_ class(my_curren t_instance, *ohers_args)

          i feel quite unhappy with the return list(self) which for me is rather a
          call to the builtin function list() upon the current instance , how could i
          write this particular :

          super_class.att ribute_containi ng_content_of(m y_current_insta nce)

          also, which are the method to overwrite for a class subclassing a list to
          automatically call the load() method if the list is empty ?

          thx for your help.


          Comment

          Working...