Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107742
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Christopher Reimer <christopher_reimer@yahoo.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Differences between Class(Object) and Class(Dict) for dictionary usage? |
| Date | Wed, 27 Apr 2016 18:12:02 -0700 |
| Lines | 69 |
| Message-ID | <mailman.160.1461811500.32212.python-list@python.org> (permalink) |
| References | <5720357B.4060009@icloud.com> <1461729360.3276080.590752673.7A6E1041@webmail.messagingengine.com> <57216362.8060209@yahoo.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de pj+1Maqo4C2rh+A5Lf0Nmg+1hgrFmMk/AanZnEZj2AzQ== |
| Return-Path | <christopher_reimer@yahoo.com> |
| 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; 'header:In-Reply-To:1': 0.24; "i've": 0.25; 'header:User-Agent:1': 0.26; 'chris': 0.26; 'checking': 0.27; 'pieces': 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; 'subject:) ': 0.32; 'class': 0.33; 'accessible': 0.33; 'false': 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; 'thank': 0.38; 'mean': 0.38; 'rather': 0.39; 'to:addr:python.org': 0.40; 'charset:windows-1252': 0.62; 'skip:n 10': 0.62; 'different': 0.63; 'benefit': 0.66; 'decided': 0.66; 'received:98.137': 0.67; 'received:98.136': 0.70; 'received:gq1.yahoo.com': 0.72; 'received:mail.gq1.yahoo.com': 0.72; 'received:bullet.mail.gq1.yahoo.com': 0.84; 'subject:Class': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1461805922; bh=dwGSGBOK5h2XabJCIUtaG/ysBYwzlS8pcqbfRj8ZaxI=; h=Subject:To:References:From:Date:In-Reply-To:From:Subject; b=qGZRaVMPCchpp7kJR3/KfdGH8mzeRM87LbFrB+XKRg5L04GHp5PqBlyE9fFVYkinSQdoWnzlmcqQALhFvgFem5OUMQxu5a3P9pFfUZJTh/PdgQnvLZAh3V2vOEddjNFvv/424euHd0n6uTKkgVfBupACSjWEW3B9oAXVFytp2RAKeD7eQJuV+JbIjTfi5du0Ydl/OKdRA7Gw6JZQpv+WQt3KH965MIrXH7pGMvf0QRwNGgFiJ7k7i+qSGhonydTJxJq/eFTfL3ZgeHVeXGv/TEtiQtoJwaMJNEWNnUxKpMIg6SLYJ/O+qjCoKBXxnlpZLVGRY3C3Pgkgpqmkwa7feg== |
| X-Yahoo-Newman-Id | 550727.74084.bm@smtp214.mail.gq1.yahoo.com |
| X-Yahoo-Newman-Property | ymail-3 |
| X-YMail-OSG | R87E4nYVM1nIfeWyT0Dijc7NPQ46R4M1Mq0FoU3mtjarn_e ooTMNjM0VxaHKg4mFgGQolLMKUHHU.AZw9r06bgEzbStt0HBScZQu1AZKjIT PkRJoIqINE2glVAlCQLYwfd0PGcTrJe3aqZC23mMXD.IIzI6Hy.kOjtUaUwV C8ADDE5rwpojbGx6D7MsTuec84z9PMfi0v8cny571H4OhCxJ1LsiS_H7scwP NsWw15VIX8Ht6qdfifrt1unsd5zuwbkKCtYnt4Qo.sYmwd_jK_6AYMTL12qY 9Ku2kjhWckrPQGGhzLJeu8sChvloyIHUK_zEk.xOZAdDC6zp5iUo1QI32jeF 0a0CmNgw78b14.arxEJ4kkHRjd3m_mB0D_H8fhK87INZLOvqGibG42oPQk8m tljGHxqy7o2dmh_cOwkamqzWlgDeQB21T8pzhhG0FJbsnxRKMPLTkR8J426z 07rpDNWQyvPxVxfO4z40uAcaCpWOvKx_YB6UufJfAmrAncqD8hQZ7K8KCCEW v3821K9mz6AJFVZB_RPNnpjqqORcJHGYagrlYPkuhkrDtYBc- |
| X-Yahoo-SMTP | YIHN9ACswBBhMpEJrz3z.xM48wsp_7bFhrtzteNj8Q-- |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.2 |
| In-Reply-To | <1461729360.3276080.590752673.7A6E1041@webmail.messagingengine.com> |
| X-Mailman-Approved-At | Wed, 27 Apr 2016 22:44:59 -0400 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.22 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| X-Mailman-Original-Message-ID | <57216362.8060209@yahoo.com> |
| X-Mailman-Original-References | <5720357B.4060009@icloud.com> <1461729360.3276080.590752673.7A6E1041@webmail.messagingengine.com> |
| Xref | csiph.com comp.lang.python:107742 |
Show key headers only | View raw
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Differences between Class(Object) and Class(Dict) for dictionary usage? Christopher Reimer <christopher_reimer@yahoo.com> - 2016-04-27 18:12 -0700
csiph-web