Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'method,': 0.05; '*the': 0.09; 'arguments,': 0.09; 'be:': 0.09; 'instance.': 0.09; 'subject:method': 0.09; 'method.': 0.15; '1),': 0.16; 'chess': 0.16; 'move?': 0.16; 'cc:addr:python-list': 0.16; '(i.e.': 0.17; 'wrote:': 0.18; 'instance': 0.18; 'position,': 0.19; 'slightly': 0.19; 'written': 0.19; '(which': 0.19; 'seems': 0.20; 'cheers,': 0.20; 'cc:no real name:2**0': 0.21; "doesn't": 0.22; 'header:In- Reply-To:1': 0.22; 'itself,': 0.23; 'wrong?': 0.23; 'subject:use': 0.24; 'creating': 0.25; 'obviously': 0.25; 'cc:2**0': 0.26; 'code': 0.26; 'figure': 0.26; 'says': 0.27; "i'm": 0.28; 'message- id:@mail.gmail.com': 0.29; 'class': 0.29; 'cc:addr:python.org': 0.29; 'pm,': 0.29; '(and': 0.30; 'basically,': 0.30; 'really,': 0.30; 'skip:\xc2 20': 0.30; 'typeerror:': 0.30; 'chris': 0.30; 'subject:?': 0.31; 'actually': 0.31; 'skip:( 20': 0.31; 'thu,': 0.32; "i've": 0.32; 'yet': 0.32; "isn't": 0.33; 'there': 0.33; 'it.': 0.33; 'instead': 0.33; 'question,': 0.34; 'wondering': 0.34; 'curious': 0.34; 'purely': 0.34; 'skip:@ 10': 0.34; 'probably': 0.35; 'be.': 0.35; 'regular': 0.35; 'received:209.85.214': 0.36; 'question': 0.36; 'but': 0.37; 'received:google.com': 0.37; 'using': 0.37; 'happens': 0.38; 'skip:_ 10': 0.38; 'received:209.85': 0.38; 'doing': 0.38; 'should': 0.38; 'right,': 0.39; "i'd": 0.39; 'received:209': 0.39; 'called': 0.40; 'point': 0.40; 'being': 0.40; 'john': 0.61; 'header:Received:6': 0.61; 'your': 0.61; 'love': 0.63; 'piece': 0.66; 'works,': 0.68; 'skip:\xc2 10': 0.74; 'subject:this': 0.74; 'contrast,': 0.84; 'sender:addr:chris': 0.84; 'tied': 0.93 Received-SPF: pass (google.com: domain of chris@rebertia.com designates 10.204.136.200 as permitted sender) client-ip=10.204.136.200; Authentication-Results: mr.google.com; spf=pass (google.com: domain of chris@rebertia.com designates 10.204.136.200 as permitted sender) smtp.mail=chris@rebertia.com; dkim=pass header.i=chris@rebertia.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=5pttbqbW1tQHI6/aW3Ryofw4edzDRbc7AMBMOiU454E=; b=R7dYQGlX++Bx+jZNgOeGbbxsCDB449Mm7SOadkRP9Ytfzsido2Oj6hSIPBjPZSDmET OF+y/UDG2n2+vhDJOmOniSSymB2YLn3vcH7vP1uNGkyNzNVN5auwQYsFq1GdjywP8zWb tkVLZxFzfnoLe7VyTlH+Rf/9I2uZaUAjBFtDk= MIME-Version: 1.0 Sender: chris@rebertia.com In-Reply-To: <4136337.363.1330668021961.JavaMail.geo-discussion-forums@ynjd19> References: <4136337.363.1330668021961.JavaMail.geo-discussion-forums@ynjd19> Date: Thu, 1 Mar 2012 22:25:11 -0800 X-Google-Sender-Auth: 39tzbAny5O0F-mMurVleOz6nTcA Subject: Re: Is this the proper way to use a class method? From: Chris Rebert To: John Salerno Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Gm-Message-State: ALoCoQkPo9ayBOp1AFuE3jevVJHwVOgGAcrNPXLoxsZO+J4gaza+e7n2IuPcmaR6VEw3qJC25WdA Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1330669514 news.xs4all.nl 6975 [2001:888:2000:d::a6]:43891 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21122 On Thu, Mar 1, 2012 at 10:00 PM, John Salerno wrote: > 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 speci= fic 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. Unfortunately, no. > 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. Right, but why not just a regular instance method? The method in question doesn't operate upon *the class itself* at all (which is the raison d'etre of classmethods); it instead operates upon an instance. A staticmethod would be slightly less weird, but really, it should just be an instance method. > 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? Just make it a normal instance method (i.e. remove the `cls` argument). > 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. That's just a coincidence. Your supercall is ought to be: super().move() In contrast, super().move(self) calls the superclass instance method `move` with 2 arguments, both `self`, which just happens to work given your move() method, inside which `cls` isn't actually a class like it ought to be. > class ChessPiece: > > =C2=A0 =C2=A0def __init__(self, position, label, has_moved): > =C2=A0 =C2=A0 =C2=A0 =C2=A0try: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.position =3D (position[0], = int(position[1])) > =C2=A0 =C2=A0 =C2=A0 =C2=A0except TypeError: > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0self.position =3D position > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.label =3D label > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.has_moved =3D has_moved > > =C2=A0 =C2=A0def move(cls, self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.has_moved =3D True > > > class Pawn(ChessPiece): > > =C2=A0 =C2=A0def __init__(self, position=3DNone, label=3D1, has_moved=3DF= alse): > =C2=A0 =C2=A0 =C2=A0 =C2=A0super().__init__(position, label, has_moved) > > =C2=A0 =C2=A0def move(self): > =C2=A0 =C2=A0 =C2=A0 =C2=A0super().move(self) > =C2=A0 =C2=A0 =C2=A0 =C2=A0self.position =3D (chr(ord(self.position[0]) += 1), self.position[1] + 1) Cheers, Chris