Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33025
| 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 | <andriy.kornatskyy@live.com> |
| 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 <andriy.kornatskyy@live.com> |
| To | <steve+comp.lang.python@pearwood.info>, "python-list@python.org" <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 | <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 <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.3492.1352469747.27098.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
1. In looks-like we check features of Foo (that may be superset) of what IFoo 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, 9 Nov 2012 13:36:39 +0000 > To: python-list@python.org > > On Thu, 08 Nov 2012 18:00:58 -0700, Ian Kelly wrote: > > > On Thu, Nov 8, 2012 at 4:33 PM, Steven D'Aprano > > <steve+comp.lang.python@pearwood.info> 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? > > What's the difference? "Looks like" is a symmetric comparison, like > "equal" and "almost equal", but not "subset", "less than" etc. If x looks > like y, then necessarily y must look like x. If Katy Perry looks like > Zooey Deschanel, then it stands to reason that Zooey Deschanel looks like > Katy Perry. James Woods looks like Erwin Schroedinger, 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, looks(Spam).like(Ham) must always be the same as > looks(Ham).like(Spam), and the order of operators doesn't matter. > > But that's not what we want! And to his credit, that's not what Andriy > Kornatskyy's code actually implements. The problem is with the name, > 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, but she's not > available, so we want a replacement who is duck-type compatible with her. > The replacement doesn't need to be a Deschanel sister, but she does need > to do *at least* everything Zooey can do. If she can do more, that's > fine, but she can't do less. > > Since Katy Perry can do everything Zooey can do, PLUS she sings, we can > replace Zooey with Katy: Katy is duck-type compatible with Zooey. But we > can't replace Katy with Zooey, 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, but the function we > actually want is not symmetrical. It is actually a "subset" type > relationship: given "looks(Zooey).like(Katy)", it checks that the public > methods etc. of Zooey are a subset of the methods of Katy. That is, that > 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, it certainly isn't clear or obvious which way you have to > write the operands!) > > Consider this use-case: > > candidates = [Hilary, Jennifer, Katy] > expected = 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 = [Hilary, Jennifer, 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, that sounds English-like. But it doesn't actually > describe what the function does, it is wasteful for at least one useful > use-case, and it's not clear which order you have to supply the two > arguments. > > > > looks(Foo).like(IFoo), on the other hand, is crystal clear about which > > 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
duck typing assert Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-08 20:34 +0300
Re: duck typing assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-08 23:33 +0000
Re: duck typing assert Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-08 18:00 -0700
Re: duck typing assert Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-09 13:36 +0000
RE: duck typing assert Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-09 17:01 +0300
Re: duck typing assert Chris Angelico <rosuav@gmail.com> - 2012-11-10 01:15 +1100
RE: duck typing assert Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-09 17:37 +0300
Re: duck typing assert Chris Angelico <rosuav@gmail.com> - 2012-11-09 17:14 +1100
RE: duck typing assert Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-09 13:26 +0300
RE: duck typing assert "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-09 17:37 +0000
RE: duck typing assert Andriy Kornatskyy <andriy.kornatskyy@live.com> - 2012-11-09 20:55 +0300
csiph-web