Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'argument': 0.04; 'arguments': 0.07; 'method,': 0.07; 'speakers,': 0.09; 'typed': 0.09; 'language,': 0.11; 'interfaces': 0.15; 'java,': 0.15; 'naming': 0.16; 'principal.': 0.16; 'wrote:': 0.17; 'thu,': 0.17; 'typing': 0.17; 'trying': 0.21; 'programming': 0.23; 'class.': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.25; 'looks': 0.26; 'order.': 0.27; 'convention': 0.27; 'strongly': 0.27; 'message-id:@mail.gmail.com': 0.27; 'assert': 0.29; "d'aprano": 0.29; 'optional': 0.29; 'steven': 0.29; 'class': 0.29; 'function': 0.30; 'could': 0.32; 'to:addr:python-list': 0.33; 'languages': 0.33; 'received:google.com': 0.34; 'clear': 0.35; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'test': 0.36; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'why': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'header:Received:5': 0.40; 'easy': 0.60; 'more': 0.63; 'offer': 0.65; 'lack': 0.71; 'to:name:python': 0.84; 'hand,': 0.97 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Ufkoqf71Ekxq9x5j0D5RI7YY/gM5T8f9JuQ1UmUKlJU=; b=Bgunx8u+/OME27h4URuTyMvQ9nUIcrAPteUx6iiJtcGcFRbPrHJIWa/mJnc5c04T95 IWugeiP6f8MJz/a6Ypdkj616UkYaKG4qIwNhd2v7nGSmeamaGArVP6maThF68x9FEs/0 +P+bxzSAWb/i34ZsxtAjd7/BEY4ROQPyLQlKX+58Z2vj28xnaswpKwsshbN2yb4iVwfM dw5SdScQGrXxdGITINEw7gBB+xu4YROJWKsVlFBqV90sjWwDmgt3oV46HRNd+GSEXwn1 s51NZe5NGLmndT+OhqmmT9gqeIBPI7bOrNh+PCKanFiaqeJHNlOFL+tqschwQ7pPXjRz oT2Q== MIME-Version: 1.0 In-Reply-To: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> References: <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com> From: Ian Kelly Date: Thu, 8 Nov 2012 18:00:58 -0700 Subject: Re: duck typing assert To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352422891 news.xs4all.nl 6854 [2001:888:2000:d::a6]:35544 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32989 On Thu, Nov 8, 2012 at 4:33 PM, Steven D'Aprano wrote: > On Thu, 08 Nov 2012 20:34:58 +0300, Andriy Kornatskyy wrote: > >> People who come from strongly typed languages that offer interfaces >> often are confused by lack of one in Python. Python, being dynamic >> typing programming language, follows duck typing principal. It can as >> simple as this: >> >> assert looks(Foo).like(IFoo) > > How very cute. And I don't mean that in a good way. > > Why is this a class with a method, instead of a function that takes two > class arguments (plus any optional arguments needed)? > > looks_like(Foo, IFoo) > > is less "cute", reads better to English speakers, and much more Pythonic. > This isn't Java, not everything needs to be a class. I disagree. Does that test whether Foo looks like IFoo, or IFoo looks like Foo? Of course, given the naming convention and the example, it's easy to work out, but when you're trying to *write* that from memory, it could be a nuisance to remember the proper order. This is already a wart of isinstance and issubclass. looks(Foo).like(IFoo), on the other hand, is crystal clear about which argument is which.