new-style classes and len method

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

    #1

    new-style classes and len method

    Hi there.

    I'm trying to use new-style classes, but there is something i'm
    obviously missing

    here it is :

    class Data(list):
    __slots__ = ["width", "height", "label"]

    def __init__(self,w idth,height,lab el=None):
    list.__init__(s elf)
    self.width = width
    self.height = height
    self.label = label

    def clear(cls):
    while len(cls) > 0: del cls[0]
    return
    clear = classmethod(cle ar)

    #> d = Data(2,2)
    #> d.clear()
    TypeError: len() of unsized object

    off course it was working with :

    [...]
    def clear(self):
    while len(self) > 0: del self[0]
    return

    So, I guess you can't use "cls" as a replacement for "self". So, what
    do I have to use ???

  • Thomas Girod

    #2
    Re: new-style classes and len method

    Or maybe I'm mixing up what we call a "classmetho d" with what we could
    call an "instance method" ?

    Comment

    • Paul McGuire

      #3
      Re: new-style classes and len method

      "Thomas Girod" <girodt@gmail.c om> wrote in message
      news:1144936961 .203493.169840@ v46g2000cwv.goo glegroups.com.. .[color=blue]
      > Hi there.
      >
      > I'm trying to use new-style classes, but there is something i'm
      > obviously missing
      >
      > here it is :
      >
      > class Data(list):
      > __slots__ = ["width", "height", "label"]
      >
      > def __init__(self,w idth,height,lab el=None):
      > list.__init__(s elf)
      > self.width = width
      > self.height = height
      > self.label = label
      >
      > def clear(cls):
      > while len(cls) > 0: del cls[0]
      > return
      > clear = classmethod(cle ar)
      >
      > #> d = Data(2,2)
      > #> d.clear()
      > TypeError: len() of unsized object
      >
      > off course it was working with :
      >
      > [...]
      > def clear(self):
      > while len(self) > 0: del self[0]
      > return
      >
      > So, I guess you can't use "cls" as a replacement for "self". So, what
      > do I have to use ???
      >[/color]

      Um, why do you think clear() needs to be a classmethod? Isn't it supposed
      to work on an instance of Data? If you just remove that "clear =
      classmethod(cle ar)" statement, I think this works as you expect.

      (Although your implementation of clear() will work correctly, you might
      consider the more efficient "del self[:]" slice operation, instead of your
      while loop.)


      Comment

      • Ben C

        #4
        Re: new-style classes and len method

        On 2006-04-13, Thomas Girod <girodt@gmail.c om> wrote:[color=blue]
        > Hi there.
        >
        > I'm trying to use new-style classes, but there is something i'm
        > obviously missing
        >
        > here it is :
        >
        > class Data(list):
        > __slots__ = ["width", "height", "label"]
        >
        > def __init__(self,w idth,height,lab el=None):
        > list.__init__(s elf)
        > self.width = width
        > self.height = height
        > self.label = label
        >
        > def clear(cls):
        > while len(cls) > 0: del cls[0]
        > return
        > clear = classmethod(cle ar)
        >
        > #> d = Data(2,2)
        > #> d.clear()
        > TypeError: len() of unsized object
        >
        > off course it was working with :
        >
        > [...]
        > def clear(self):
        > while len(self) > 0: del self[0]
        > return
        >
        > So, I guess you can't use "cls" as a replacement for "self". So, what
        > do I have to use ???[/color]

        You can use cls in a classmethod to refer to the class. You can use any
        name you want (well so long as it's not a "reserved word" I suppose).
        The same is true of self, it's just a param name (quite unlike
        JavaScript's "this"...).

        But I don't think it makes sense for clear to be a classmethod, since
        you presumably want a method that clears instances of lists?

        The error is because you're trying to take the len of the class itself,
        not the len of an instance of it.

        What you had before was OK. Although you don't need to write the loop,
        just del self[:] will do, and you can leave out the return statement if
        you want.

        Comment

        • Thomas Girod

          #5
          Re: new-style classes and len method

          It's alright I found where my mistake came from. I was misunderstandin g
          the meaning of "classmetho d", thinking of it as an instance method.

          Comment

          • bruno at modulix

            #6
            Re: new-style classes and len method

            Thomas Girod wrote:[color=blue]
            > Or maybe I'm mixing up what we call a "classmetho d" with what we could
            > call an "instance method" ?
            >[/color]
            That's what I was about to point out !-)
            [color=blue]
            > class Data(list):
            > __slots__ = ["width", "height", "label"]
            >
            > def __init__(self,w idth,height,lab el=None):
            > list.__init__(s elf)
            > self.width = width
            > self.height = height
            > self.label = label
            >
            > def clear(cls):
            > while len(cls) > 0: del cls[0]
            > return
            > clear = classmethod(cle ar)[/color]

            What about this ?

            def clear(self):
            del self[:]

            As it's name (and signature) implies, a classmethod works on a class,
            not on an instance of the class. Here, you are trying to compute the
            length of *class* Data.


            --
            bruno desthuilliers
            python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
            p in 'onurb@xiludom. gro'.split('@')])"

            Comment

            • bruno at modulix

              #7
              Re: new-style classes and len method

              Thomas Girod wrote:[color=blue]
              > It's alright I found where my mistake came from. I was misunderstandin g
              > the meaning of "classmetho d", thinking of it as an instance method.[/color]

              Given that 'instance methods' being most of the time attributes of the
              class object, such a confusion is not so surprising !-)

              --
              bruno desthuilliers
              python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
              p in 'onurb@xiludom. gro'.split('@')])"

              Comment

              Working...