Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ethan Furman Newsgroups: comp.lang.python Subject: Re: Differences between Class(Object) and Class(Dict) for dictionary usage? Date: Wed, 27 Apr 2016 20:05:27 -0700 Lines: 65 Message-ID: References: <57216399.1050702@icloud.com> <57217DF7.1030509@stoneleaf.us> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de yLFufJTBp2qAyEfIEQhvXQSo0HZyrddxzrol8JL7x+YA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'method.': 0.05; 'attributes': 0.07; 'constructor': 0.07; 'method,': 0.07; 'sanity': 0.07; '@property': 0.09; 'below).': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'message-id:@stoneleaf.us': 0.09; 'subclasses': 0.09; 'subject:Object': 0.09; 'stored': 0.10; 'def': 0.13; 'subject: \n ': 0.15; 'variables': 0.15; 'kings': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'self._name': 0.16; 'subject:between': 0.16; 'subject:usage': 0.16; 'wrote:': 0.16; '(see': 0.20; 'color,': 0.22; 'pass': 0.22; 'code,': 0.23; 'header :In-Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'checking': 0.27; 'pieces': 0.27; 'values': 0.28; 'dictionary': 0.29; 'notation': 0.29; 'pickle': 0.29; '~ethan~': 0.29; 'raise': 0.29; 'code': 0.30; "can't": 0.32; 'skip:_ 10': 0.32; 'implement': 0.32; 'subject:) ': 0.32; 'class': 0.33; 'accessible': 0.33; 'instances': 0.33; 'false': 0.35; 'instance': 0.35; 'saved': 0.35; 'loaded': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'feedback': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'more': 0.63; 'different': 0.63; 'better.': 0.66; 'decided': 0.66; 'results': 0.66; 'color': 0.67; 'worth': 0.67; 'for).': 0.91; 'subject:Class': 0.91 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 In-Reply-To: <57216399.1050702@icloud.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: <57217DF7.1030509@stoneleaf.us> X-Mailman-Original-References: <57216399.1050702@icloud.com> Xref: csiph.com comp.lang.python:107744 On 04/27/2016 06:12 PM, Christopher Reimer wrote: > After considering the feedback I got for sanity checking my code, I've > decided to simplify the base class for the chess pieces (see code > below). All the variables are stored inside a dictionary with most > values accessible through properties. A custom dictionary can be loaded > through the constructor and or saved out through the fetch_state method. > The subclasses only have to implement the is_move_valid method, which is > different for each type of chess piece. Much better. Here's my take on it: class Piece(object): def __init__(self, color, position): self._color = color self._first_move = True self._initial_position = position self._move_count = 0 self._name = color.title() + ' ' + self.__class__.__name__ self._notation = color.title()[:1] + self.__class__.__name__[:1] self._position = position @property def color(self): return self._color def is_move_valid(self, new_position, board_state): raise NotImplementedError @property def move_count(self): return self._move_count @property def name(self): return self._name @property def notation(self): return self._notation @property def position(self): return self._position @position.setter def position(self, position): self._position = position if self._first_move: self._first_move = False self._move_count += 1 Now all the attributes are, well, attributes of the instance (that's what instances are for). I ripped out the fetch_state because that will take more work -- you can't pass a Pawn's saved state in to Piece and get the results you want. pickle is worth looking at for saving/restoring. Also, your code will give the same notation to Kings and Knights. -- ~Ethan~