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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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