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


Groups > comp.lang.python > #86454

Re: Python Worst Practices

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.049
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; 'subject:Python': 0.06; 'suppose': 0.07; 'sure,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'dots': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'worst': 0.16; 'wrote:': 0.18; 'thu,': 0.19; '(the': 0.22; 'feb': 0.22; 'example': 0.22; 'cc:addr:python.org': 0.22; "shouldn't": 0.24; 'cc:2**0': 0.24; 'this:': 0.26; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'along': 0.30; 'code': 0.31; 'lines': 0.31; 'bunch': 0.31; "d'aprano": 0.31; 'enforce': 0.31; 'object.': 0.31; 'shelf': 0.31; 'steven': 0.31; 'file': 0.32; 'class': 0.32; 'another': 0.32; 'sense': 0.34; 'skip:_ 10': 0.34; "i'd": 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'there': 0.35; 'too': 0.37; 'two': 0.37; 'list.': 0.37; 'rather': 0.38; 'money.': 0.60; 'times': 0.62; 'reach': 0.63; 'card': 0.63; 'more': 0.64; '26,': 0.68; 'paper': 0.75; 'sole': 0.78; '2015': 0.84; 'dog.': 0.84; 'pocket': 0.84; 'subject:Practices': 0.84; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=heUy0qci6BWiYVqeDUUEtGMyibd0HRay9Izgx7XiDeo=; b=b1Efol8gj0mipFX4DnXbR0PgAO3cNSkYHWfy9ZhI0TwVJsj817+NpNEkcPWsTgVVqi N73FpnselakIXcfCT+IUswX0hBjf1wykwSdIOJX8GW0oj40PyJbK2PcbGnC0fXBwUKKz lEnJV32R87irfi3jqelHMVF3Zp5DiQQ7gOCFFvd3bkCw+pm7sGP29nWl0KiSXnUzNIUy uTgHhXA+RX/rKdV867gRe81dea5s+N+cdbP5aTN9rjDby786IyXdrdX5lTRf29vXCA8s M+wZp4mrHzqNgRfWmtgRRa+xFovC56hOVnrIBqFxSkGzDaaAsykSBLTh7A0eLgfs5iIm rFow==
MIME-Version 1.0
X-Received by 10.50.131.196 with SMTP id oo4mr8530080igb.2.1424910401024; Wed, 25 Feb 2015 16:26:41 -0800 (PST)
In-Reply-To <54ee60af$0$13001$c3e8da3$5496439d@news.astraweb.com>
References <mailman.19215.1424897186.18130.python-list@python.org> <54ee60af$0$13001$c3e8da3$5496439d@news.astraweb.com>
Date Thu, 26 Feb 2015 11:26:40 +1100
Subject Re: Python Worst Practices
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.19226.1424910410.18130.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1424910410 news.xs4all.nl 2878 [2001:888:2000:d::a6]:46095
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86454

Show key headers only | View raw


On Thu, Feb 26, 2015 at 10:54 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> - Violating the Rule of Demeter: don't talk to the dog's leg, talk to
>   the dog. Or another way to put it: don't let the paper boy reach
>   into your pocket for money.

I'd call that code smell, rather than an automatic worst practice. Suppose this:

class Shelf:
    def __init__(self):
        self.items = [] # Empty shelf

bookshelf = Shelf()
bookshelf.items.append(book)

To enforce Demeter, you'd have to add a bunch of methods to the Shelf
whose sole purpose is to pass word along to the items list. Sure, it
makes some design sense to say "Add this book to the shelf" rather
than "Add this book to the items on the shelf", but all those lines of
code are potential bugs, and if you have to reimplement huge slabs of
functionality, that too is code smell. So there are times when it's
correct to reach into another object.

But the times to use two dots are much rarer than the times to use one
dot (the paper boy shouldn't reach into your pocket for money, but
ThinkGeek has your credit card number on file so you can order more
conveniently), and I can't think of any example off-hand where you
would want more than three dots.

ChrisA

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


Thread

Python Worst Practices Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-25 20:45 +0000
  Re: Python Worst Practices Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-26 10:54 +1100
    Re: Python Worst Practices Chris Angelico <rosuav@gmail.com> - 2015-02-26 11:26 +1100
      Re: Python Worst Practices Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-26 23:26 +1100
        Re: Python Worst Practices Chris Angelico <rosuav@gmail.com> - 2015-02-27 00:12 +1100
  Re: Python Worst Practices wxjmfauth@gmail.com - 2015-02-25 22:46 -0800
  Re: Python Worst Practices m <mvoicem@gmail.com> - 2015-02-26 14:12 +0100
    Re: Python Worst Practices Chris Angelico <rosuav@gmail.com> - 2015-02-27 00:37 +1100

csiph-web