subclassing list

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

    #1

    subclassing list

    Hi,

    I want to subclass "list". The documentation states to prefer subclassing
    list instead of UserList. How to you clear the contents of a list subclass
    without creating a new object?

    Thanks in advance
    Uwe
  • Fuzzyman

    #2
    Re: subclassing list

    class newList(list):
    def clear(self):
    self[:] = []

    is one way.
    HTH

    Regards,

    Fuzzy
    http://www.voidspace.org.uk/python/index.shtml

    Comment

    • Richie Hindle

      #3
      Re: subclassing list


      [Uwe][color=blue]
      > How [do] you clear the contents of a list subclass
      > without creating a new object?[/color]

      Use the paranoia emoticon: "del x[:]". For example:
      [color=blue][color=green][color=darkred]
      >>> class L(list):[/color][/color][/color]
      .... pass
      ....[color=blue][color=green][color=darkred]
      >>> x = L()
      >>> x.append("Spam" )
      >>> del x[:]
      >>> x[/color][/color][/color]
      [][color=blue][color=green][color=darkred]
      >>> type(x)[/color][/color][/color]
      <class '__main__.L'>[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      with-thanks-to-Gordon-McMillan-ly y'rs,

      --
      Richie Hindle
      richie@entrian. com

      Comment

      Working...