Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Christopher Reimer Newsgroups: comp.lang.python Subject: Re: Differences between Class(Object) and Class(Dict) for dictionary usage? Date: Wed, 27 Apr 2016 18:12:57 -0700 Lines: 68 Message-ID: References: <57216399.1050702@icloud.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de P86oxD00ts0NUnYvROnZYwf3+oZkwsfirs0AzTHtcIAQ== 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; 'else:': 0.03; 'position,': 0.04; 'true,': 0.04; 'method.': 0.05; 'none:': 0.05; 'attributes': 0.07; 'constructor': 0.07; 'method,': 0.07; 'sanity': 0.07; "'name':": 0.09; '@property': 0.09; 'below).': 0.09; 'subclasses': 0.09; 'subject:Object': 0.09; 'stored': 0.10; 'def': 0.13; 'subject: \n ': 0.15; 'variables': 0.15; "'class':": 0.16; "'color':": 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'self._state': 0.16; 'subject:between': 0.16; 'subject:usage': 0.16; 'wrote:': 0.16; '(see': 0.20; 'color,': 0.22; 'code,': 0.23; "i've": 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'checking': 0.27; 'pieces': 0.27; 'received:17': 0.27; "skip:' 10": 0.28; 'values': 0.28; 'dictionary': 0.29; 'raise': 0.29; 'code': 0.30; 'skip:_ 10': 0.32; 'implement': 0.32; 'received:10.0.0': 0.32; 'subject:) ': 0.32; 'class': 0.33; 'accessible': 0.33; 'received:10.0': 0.34; 'false': 0.35; 'saved': 0.35; 'loaded': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'feedback': 0.38; 'thank': 0.38; 'mean': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'header:MIME-version:1': 0.60; 'skip:n 10': 0.62; 'different': 0.63; 'benefit': 0.66; 'decided': 0.66; 'subject:Class': 0.91 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:,, definitions=2016-04-27_14:,, signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 clxscore=1015 suspectscore=1 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1510270003 definitions=main-1604280014 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=icloud.com; s=4d515a; t=1461805982; bh=dwGSGBOK5h2XabJCIUtaG/ysBYwzlS8pcqbfRj8ZaxI=; h=To:From:Subject:Message-id:Date:MIME-version:Content-type; b=okn6z+nAuT4wKFcVOYy4Iep8/1moK8yXZuvbna+dMPUIycDZqg7c57Dfxf96q2v3n mF6gQvRs8VDD2nOgYUQAw6/LOYm3V3QS/jJZn2/ri/kk1cVuAt5OutYudiYxGfr1my WFAQg8uzNKtzZ6vlV9gf3zwqHr2zECcAkCqQy5JhNTL44s94BVSnnTjzVzWJ9fGkaw MsC2/LSbg1bkAhUn+ePjYLqjBDwTZonduttPvaYlnupJkLL21ObVwh2ccByp25QFUa os1VXhCxnmohYv3KVSv+SXXKfQp3hFzISZfTcCcy5k8+KlXffOIQ3fCiIMYNRwNpVT vFMbd8wkPA98A== 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: <57216399.1050702@icloud.com> Xref: csiph.com comp.lang.python:107737 On 4/26/2016 8:56 PM, Random832 wrote: > what exactly do you mean by property decorators? If you're just > accessing them in a dictionary what's the benefit over having the > values be simple attributes rather than properties? 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. Thank you, Chris R. class Piece(object): def __init__(self, color, position, state=None): if state is None: self._state = { 'class': self.__class__.__name__, 'color': color, 'first_move': True, 'initial_position': position, 'move_count': 0, 'name': color.title() + ' ' + self.__class__.__name__, 'notation': color.title()[:1] + self.__class__.__name__[:1], 'position': position } else: self._state = state @property def color(self): return self._state['color'] def fetch_state(self): return self._state def is_move_valid(self, new_position, board_state): raise NotImplementedError @property def move_count(self): return self._state['move_count'] @property def name(self): return self._state['name'] @property def notation(self): return self._state['notation'] @property def position(self): return self._state['position'] @position.setter def position(self, position): self._state['position'] = position if self._state['first_move']: self._state['first_move'] = False self._state['move_count'] += 1