Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; 'argument': 0.04; '(even': 0.05; 'arguments': 0.07; 'method,': 0.07; 'operand': 0.07; 'suppose': 0.07; 'does,': 0.09; 'speakers,': 0.09; 'subset': 0.09; 'typed': 0.09; 'def': 0.10; 'language,': 0.11; 'do,': 0.15; 'interfaces': 0.15; 'java,': 0.15; 'instantiate': 0.16; 'principal.': 0.16; 'sense,': 0.16; 'suggesting': 0.16; 'symmetric': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; 'candidates': 0.17; 'wrote:': 0.17; 'certainly': 0.17; 'thu,': 0.17; 'typing': 0.17; 'tests': 0.18; '>>>': 0.18; '+0000': 0.20; 'to:name:python- list@python.org': 0.20; 'skip:- 40': 0.21; 'thanks.': 0.21; 'do.': 0.21; 'fine,': 0.22; 'programming': 0.23; 'class.': 0.23; 'this:': 0.23; 'to:2**1': 0.23; 'pass': 0.25; 'least': 0.25; 'header:In- Reply-To:1': 0.25; 'looks': 0.26; 'skip:" 20': 0.26; '[1]': 0.27; 'implemented': 0.27; 'replace': 0.27; 'strongly': 0.27; "doesn't": 0.28; '-0700,': 0.29; 'argue': 0.29; 'arguments.': 0.29; 'assert': 0.29; 'comparison': 0.29; "d'aprano": 0.29; 'optional': 0.29; 'steven': 0.29; 'way?': 0.29; 'date:': 0.29; 'url:mailman': 0.29; 'class': 0.29; 'actively': 0.30; 'checks': 0.30; 'fri,': 0.30; 'function': 0.30; 'compatible': 0.30; 'code': 0.31; 'point': 0.31; 'url:python': 0.32; 'more,': 0.32; 'not.': 0.32; 'url:listinfo': 0.32; 'from:addr:live.com': 0.33; 'function.': 0.33; 'instances': 0.33; 'stands': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'know.': 0.33; 'languages': 0.33; 'another': 0.33; "can't": 0.34; 'wrong': 0.34; 'along': 0.35; 'clear': 0.35; 'false': 0.35; 'nov': 0.35; 'expected': 0.35; 'pm,': 0.35; 'really': 0.36; 'subject:': 0.36; 'but': 0.36; 'url:org': 0.36; 'email addr:python.org': 0.36; 'method': 0.36; 'useful': 0.36; 'test': 0.36; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'from:': 0.38; 'mean': 0.38; 'skip:l 20': 0.38; 'some': 0.38; 'description': 0.39; 'instead': 0.39; 'to:addr:python.org': 0.39; 'takes': 0.39; 'url:12': 0.40; 'url:mail': 0.40; '(that': 0.62; 'email name:python-list': 0.62; 'necessarily': 0.63; 'more': 0.63; 'making': 0.64; 'offer': 0.65; 'matter.': 0.65; 'url:uploads': 0.65; 'lack': 0.71; 'now:': 0.71; 'obvious': 0.71; 'sounds': 0.71; 'url:jpg': 0.84; 'around,': 0.84; 'guessed': 0.84; 'offers.': 0.84; 'only:': 0.84; 'think...': 0.84; 'want!': 0.91; 'url:wp-content': 0.93; 'hand,': 0.97; 'charset:windows-1251': 0.97 X-Originating-IP: [194.44.213.194] From: Andriy Kornatskyy To: , "python-list@python.org" Subject: RE: duck typing assert Date: Fri, 9 Nov 2012 17:01:19 +0300 Importance: Normal In-Reply-To: <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> References: , <509c412d$0$29980$c3e8da3$5496439d@news.astraweb.com>, , <509d06e7$0$29980$c3e8da3$5496439d@news.astraweb.com> Content-Type: text/plain; charset="windows-1251" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginalArrivalTime: 09 Nov 2012 14:01:19.0428 (UTC) FILETIME=[B15CB040:01CDBE82] 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: 167 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352469747 news.xs4all.nl 6854 [2001:888:2000:d::a6]:45828 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33025 1. In looks-like we check features of Foo (that may be superset) of what IF= oo offers. assert looks(Foo).like(IFoo) 2. We can check if Foo is limited to IFoo only: assert looks(IFoo).like(Foo) So it valid to have both asserts. Thanks. Andriy Kornatskyy ---------------------------------------- > From: steve+comp.lang.python@pearwood.info > Subject: Re: duck typing assert > Date: Fri=2C 9 Nov 2012 13:36:39 +0000 > To: python-list@python.org > > On Thu=2C 08 Nov 2012 18:00:58 -0700=2C Ian Kelly wrote: > > > On Thu=2C Nov 8=2C 2012 at 4:33 PM=2C Steven D'Aprano > > wrote: > >> On Thu=2C 08 Nov 2012 20:34:58 +0300=2C Andriy Kornatskyy wrote: > >> > >>> People who come from strongly typed languages that offer interfaces > >>> often are confused by lack of one in Python. Python=2C being dynamic > >>> typing programming language=2C 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=2C instead of a function that takes = two > >> class arguments (plus any optional arguments needed)? > >> > >> looks_like(Foo=2C IFoo) > >> > >> is less "cute"=2C reads better to English speakers=2C and much more > >> Pythonic. This isn't Java=2C not everything needs to be a class. > > > > I disagree. Does that test whether Foo looks like IFoo=2C or IFoo looks > > like Foo? > > What's the difference? "Looks like" is a symmetric comparison=2C like > "equal" and "almost equal"=2C but not "subset"=2C "less than" etc. If x l= ooks > like y=2C then necessarily y must look like x. If Katy Perry looks like > Zooey Deschanel=2C then it stands to reason that Zooey Deschanel looks li= ke > Katy Perry. James Woods looks like Erwin Schroedinger=2C and Erwin > Schroedinger looks like James Woods. > > http://tvrefill.com/wp-content/uploads/2010/12/zooey-deschanel.jpg > > http://cheezburger.com/6704400128 > > > So in that sense=2C looks(Spam).like(Ham) must always be the same as > looks(Ham).like(Spam)=2C and the order of operators doesn't matter. > > But that's not what we want! And to his credit=2C that's not what Andriy > Kornatskyy's code actually implements. The problem is with the name=2C > which is actively misleading by suggesting a symmetrical comparison for > one which is not symmetrical. > > Suppose we want to make a movie with Zooey Deschanel=2C but she's not > available=2C so we want a replacement who is duck-type compatible with he= r. > The replacement doesn't need to be a Deschanel sister=2C but she does nee= d > to do *at least* everything Zooey can do. If she can do more=2C that's > fine=2C but she can't do less. > > Since Katy Perry can do everything Zooey can do=2C PLUS she sings=2C we c= an > replace Zooey with Katy: Katy is duck-type compatible with Zooey. But we > can't replace Katy with Zooey=2C because Zooey can't sing.[1] > > (I think... I really don't actually know if Zooey Deschanel can sing or > not. Just go along with the example.) > > The point I am making is that "looks like" is not a good description for > this function. "Looks like" must be symmetrical=2C but the function we > actually want is not symmetrical. It is actually a "subset" type > relationship: given "looks(Zooey).like(Katy)"=2C it checks that the publi= c > methods etc. of Zooey are a subset of the methods of Katy. That is=2C tha= t > instances of Katy can be used instead of instances of Zooey. > > Or is it the other way? Damned if I know. Let's find out: > > class Zooey: > def act(self): pass > > class Katy: > def act(self): pass > def sing(self): pass > > > py> looks(Zooey).like(Katy) > __main__:2: UserWarning: 'sing': is missing. > False > > I guessed wrong. The looks.like method as implemented tests that the > right-hand size operand is a subset of the right-hand-side: > > py> looks(Katy).like(Zooey) > True > > > I argue that this is the wrong way around. (Even if it isn't the wrong > way around=2C it certainly isn't clear or obvious which way you have to > write the operands!) > > Consider this use-case: > > candidates =3D [Hilary=2C Jennifer=2C Katy] > expected =3D looks(Zooey) # instantiate the looks class once only > for actor in candidates: > if expected.like(actor): > make_movie_with(actor()) > > > That's much nicer and more efficient than the way you have to write it > now: > > candidates =3D [Hilary=2C Jennifer=2C Katy] > for actor in candidates: > # instantiate the looks class every time we want to test another class > if looks(actor).like(Zooey): > make_movie_with(actor()) > > > So... it's a cute name=2C that sounds English-like. But it doesn't actual= ly > describe what the function does=2C it is wasteful for at least one useful > use-case=2C and it's not clear which order you have to supply the two > arguments. > > > > looks(Foo).like(IFoo)=2C on the other hand=2C is crystal clear about wh= ich > > argument is which. > > I hope that by now you can see why I say that it is as clear as mud. > > > > > > [1] Some people might argue that neither can Katy Perry. > > > -- > Steven > -- > http://mail.python.org/mailman/listinfo/python-list =