Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #21121

Is this the proper way to use a class method?

From John Salerno <johnjsal@gmail.com>
Newsgroups comp.lang.python
Subject Is this the proper way to use a class method?
Date 2012-03-01 22:00 -0800
Organization http://groups.google.com
Message-ID <4136337.363.1330668021961.JavaMail.geo-discussion-forums@ynjd19> (permalink)

Show all headers | View raw


This is purely for fun and learning, so I know there are probably better ways of creating a chess program. Right now I'm just curious about my specific question, but I'd love to hear any other advice as well.

Basically, I'm wondering if I'm using the class method properly with the move method. The reason I did it this way is because every chess piece will obviously have its own move method, yet they will all need to mark the piece as moved, so I figure that's best written once in the superclass.

This works, but doing it this way seems weird, since the point of a class method is that it can be called by the class itself, even before instances have been created. Yet the way I've implemented it, it is necessarily tied to being called on an instance. Is this wrong? Is there a better way to do what I'm doing with move?

Also, do I need the @classmethod decorator? The book I'm reading says to use it (and @staticmethod), but the following code works without it.

Thanks.



class ChessPiece:

    def __init__(self, position, label, has_moved):
        try:
            self.position = (position[0], int(position[1]))
        except TypeError:
            self.position = position
        self.label = label
        self.has_moved = has_moved

    def move(cls, self):
        self.has_moved = True


class Pawn(ChessPiece):

    def __init__(self, position=None, label=1, has_moved=False):
        super().__init__(position, label, has_moved)

    def move(self):
        super().move(self)
        self.position = (chr(ord(self.position[0]) + 1), self.position[1] + 1)
            

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Is this the proper way to use a class method? John Salerno <johnjsal@gmail.com> - 2012-03-01 22:00 -0800
  Re: Is this the proper way to use a class method? Chris Rebert <clp2@rebertia.com> - 2012-03-01 22:25 -0800
    Re: Is this the proper way to use a class method? John Salerno <johnjsal@gmail.com> - 2012-03-01 23:16 -0800
      Re: Is this the proper way to use a class method? Ian Kelly <ian.g.kelly@gmail.com> - 2012-03-02 01:04 -0700
      Re: Is this the proper way to use a class method? Chris Rebert <clp2@rebertia.com> - 2012-03-02 00:08 -0800
        Re: Is this the proper way to use a class method? John Salerno <johnjsal@gmail.com> - 2012-03-02 10:51 -0800
        Re: Is this the proper way to use a class method? John Salerno <johnjsal@gmail.com> - 2012-03-02 10:51 -0800
    Re: Is this the proper way to use a class method? John Salerno <johnjsal@gmail.com> - 2012-03-01 23:16 -0800

csiph-web