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


Groups > comp.lang.python > #53184

Interface and duck typing woes

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <joe.fbs.junior@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.040
X-Spam-Evidence '*H*': 0.92; '*S*': 0.00; 'python,': 0.02; 'method.': 0.07; 'wrong,': 0.09; 'def': 0.12; 'pythonic': 0.16; 'thanks,': 0.17; 'stack': 0.19; 'library,': 0.24; 'pass': 0.26; 'asking': 0.27; 'joe': 0.30; 'message-id:@mail.gmail.com': 0.30; 'lines': 0.31; 'question:': 0.31; 'trace': 0.31; 'class': 0.32; 'skip:_ 10': 0.34; 'there,': 0.34; 'problem': 0.35; 'hundreds': 0.35; 'usual': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'too': 0.37; 'implement': 0.38; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'course': 0.61; 'simple': 0.61; 'happen': 0.63; 'myself': 0.63; 'situation': 0.65; 'here': 0.66
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=rskM2Qo75ES8bAuqNejqVKEiY7TANO5irzktI7sq2aQ=; b=Kr+8WkKCnyDXr0R4fnJgVYqcQqb1IAFd2D83yw+EAgZel3nscJDxmTebINnt+4CEnt S7QMgjFM18vjaOC0Y8oyvMOH5fVs6HunkNM7BkzybkdOWv3gdSgIrgZVe60Eu6G8bB1B VoFD022a8ZfYgkC5kXhSYTSDqHxnN9V2XlNhVQO2Iuyo1EsnYbkjRVaS2zbZgkp7a9dp G3eA0DwagOA1ZgWTFLcn1znz4ImJ0uA71jkm9AcQ9t4bkQY7+YsIFZTDeOQc2tLdfExf BpZ52WG3HMboCms+2j8zBAOIVxiQKiq5UjEONQHu4WRlBBIxwhFLAgoe0O3GQydAVWpH Ercw==
MIME-Version 1.0
X-Received by 10.49.121.37 with SMTP id lh5mr32486578qeb.40.1377724162566; Wed, 28 Aug 2013 14:09:22 -0700 (PDT)
Date Wed, 28 Aug 2013 18:09:22 -0300
Subject Interface and duck typing woes
From Joe Junior <joe.fbs.junior@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-Mailman-Approved-At Wed, 28 Aug 2013 23:12:30 +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.327.1377724351.19984.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1377724351 news.xs4all.nl 15915 [2001:888:2000:d::a6]:55977
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:53184

Show key headers only | View raw


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

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


Thread

Interface and duck typing woes Joe Junior <joe.fbs.junior@gmail.com> - 2013-08-28 18:09 -0300
  Re: Interface and duck typing woes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-28 23:24 +0000
  Re: Interface and duck typing woes Nobody <nobody@nowhere.com> - 2013-08-29 10:41 +0100
    Re: Interface and duck typing woes Joe Junior <joe.fbs.junior@gmail.com> - 2013-08-29 09:40 -0300
      Re: Interface and duck typing woes alex23 <wuwei23@gmail.com> - 2013-08-30 10:14 +1000
      Re: Interface and duck typing woes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-30 02:42 +0000
        Re: Interface and duck typing woes Roy Smith <roy@panix.com> - 2013-08-30 06:35 -0400
          Re: Interface and duck typing woes Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-31 00:13 +0000
            Re: Interface and duck typing woes Ned Batchelder <ned@nedbatchelder.com> - 2013-08-30 20:45 -0400
            Re: Interface and duck typing woes Joshua Landau <joshua@landau.ws> - 2013-09-01 00:18 +0100
            Re: Interface and duck typing woes Roy Smith <roy@panix.com> - 2013-08-31 20:52 -0400
    Re: Interface and duck typing woes Chris Angelico <rosuav@gmail.com> - 2013-08-29 23:07 +1000
    Re: Interface and duck typing woes Joe Junior <joe.fbs.junior@gmail.com> - 2013-08-29 11:11 -0300
  Re: Interface and duck typing woes jussi.santti@ard.fi - 2013-08-29 22:37 -0700

csiph-web