Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #53218
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <fp2161@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'python,': 0.02; 'classes,': 0.05; 'attribute': 0.07; 'method.': 0.07; 'plenty': 0.07; 'purpose.': 0.07; 'append': 0.09; 'wrong,': 0.09; 'def': 0.12; 'creat': 0.16; 'pythonic': 0.16; 'received:192.168.0.11': 0.16; '\xe9crit': 0.16; 'thanks,': 0.17; 'hey': 0.18; 'stack': 0.19; '(the': 0.22; 'library,': 0.24; 'simpler': 0.24; 'bird': 0.26; 'possibly': 0.26; 'this:': 0.26; 'pass': 0.26; 'asking': 0.27; 'header:In-Reply-To:1': 0.27; 'lines': 0.31; 'question:': 0.31; 'trace': 0.31; 'class': 0.32; 'lists': 0.32; 'handled': 0.32; 'another': 0.32; 'skip:_ 10': 0.34; 'there,': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'problem': 0.35; 'display': 0.35; 'classes': 0.35; 'hundreds': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'method': 0.36; 'should': 0.36; 'too': 0.37; 'list': 0.37; 'email addr:python.org': 0.37; 'list.': 0.37; 'implement': 0.38; 'message-id:@gmail.com': 0.38; 'to:addr :python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'course': 0.61; 'simple': 0.61; 'header:Message-Id:1': 0.63; 'happen': 0.63; 'myself': 0.63; 'situation': 0.65; 'here': 0.66; 'email name:python-list-request': 0.91; 'story,': 0.91; '2013': 0.98 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to; bh=ceG3LJiT5CtpsSYfJBmdxk+eTKiiMkCrKF5X4Mb0bEU=; b=uQG+OU0H1YIRAbNzDRXEJksPu6EI8bZQFbwmGoTDs58PM1P9o+gL2gddTzTbK4CLXz QTsV8IPx4u0Q6Y3FWB/yq0pYKQcDbUdCdblWgYfnJEVUbSoBQKstE+ZT4cCRWL+2Dzbt pV19TrydqXMRFjDqu72oj5ccv36F+Q2PFmOPV667zJahnbDvMjlETJ0Iz9bQGPIdfgZM 5aV7goh6kyL7F6K8pVs4Hs0U/np/BXykSIweK5fjRc9ELCmmzWTIs5sNU7JsIPSf/ROD dWCK9bGBaQGRqMZwSEIEeMdk8/VPkkXTCMccoAtLmQrbgV/cKWPphmQBvsaPiYxKYyXD cFWg== |
| X-Received | by 10.194.11.67 with SMTP id o3mr3279320wjb.0.1377757887638; Wed, 28 Aug 2013 23:31:27 -0700 (PDT) |
| Content-Type | text/plain; charset=iso-8859-1 |
| Mime-Version | 1.0 (Mac OS X Mail 6.5 \(1508\)) |
| Subject | Re: Interface and duck typing woes |
| From | Fabrice POMBET <fp2161@gmail.com> |
| In-Reply-To | <mailman.2353.1377730619.19983.python-list@python.org> |
| Date | Thu, 29 Aug 2013 08:31:25 +0200 |
| Content-Transfer-Encoding | quoted-printable |
| References | <mailman.2353.1377730619.19983.python-list@python.org> |
| To | python-list@python.org |
| X-Mailer | Apple Mail (2.1508) |
| X-Mailman-Approved-At | Thu, 29 Aug 2013 09:09:31 +0200 |
| 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 | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.345.1377760173.19984.python-list@python.org> (permalink) |
| Lines | 79 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1377760173 news.xs4all.nl 15889 [2001:888:2000:d::a6]:50693 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:53218 |
Show key headers only | View raw
Le 29 août 2013 à 00:56, python-list-request@python.org a écrit :
"""While designing a simple library, I found myself asking a
philosophical question: to check or not to check the parameter's
interface?
I think that, considering it is Python, the usual answer would be
"no", but here is the situation that got me thinking:
class Flock:
def __init__(self):
self.ducks= []
def do_stuff(self):
for duck in self.ducks:
duck.quack()
class Duck:
def quack(self):
#quack-quack
pass
f = Flock()
d = Duck()
f.ducks.append(d)
f.do_stuff()
Ok, no big deal there, the problem is if the user forgets to implement
the quack() method. The stack trace would complain that "duck.quack()"
is wrong, but that can happen hundreds of lines after the user
actually added the object to the Flock, and it can be hard to find out
what is happening and which object is wrong.
Of course I don't want to check isistance(), I like duck typing, but
should I check if hasattr() and callable() before adding to the
container? What is the pythonic way to deal with it? Am I worrying too
much ;-)?
Thanks,
Joe"""
Hey Joe,
I am no depository of the pythonic way to think(tm) but I would create flock and inherit Duck from flock, or possibly set Flock as a method of ducks.
that would look like this:
class Flock():
def __init__(self, flock):
self.flock=flock
class Duck(Flock):
def __init(self, flock):
super().__init__(flock)
then you only need to create some functions for any object to display the lists and or dicts that you will create outside these classes, in the main or in another function...
you just instantiate them like that:
Donald=Duck('Donald')
or (rather):
flock=[]
flock.append(Duck('Donald'))
one big advantage with this method is, you can creat plenty of other bird classes and append them to a list.
Alternatively, you could have just one class Flock and then set duck as an attribute of flock, and set a list of your flock as a private attribute (the self.duck thing in your code... Well... Could be handled in a better way...) but that is another story, the one before is way simpler for your purpose.
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Re: Interface and duck typing woes Fabrice POMBET <fp2161@gmail.com> - 2013-08-29 08:31 +0200 Re: Interface and duck typing woes Steven D'Aprano <steve@pearwood.info> - 2013-08-29 07:53 +0000
csiph-web