Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'python,': 0.02; '21,': 0.07; 'welcome.': 0.07; 'python': 0.09; "'''": 0.09; '@property': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'typed': 0.09; 'def': 0.10; 'language,': 0.11; 'programmer': 0.11; 'interfaces': 0.15; 'classes:': 0.16; 'foo(object):': 0.16; 'principal.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'fix': 0.17; 'typing': 0.17; 'jan': 0.18; 'import': 0.21; 'error.': 0.21; 'programming': 0.23; 'this:': 0.23; 'user.': 0.23; 'properties': 0.24; 'pass': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; '(most': 0.27; 'strongly': 0.27; 'header:X-Complaints-To:1': 0.28; 'post': 0.28; 'assert': 0.29; 'class': 0.29; 'url:2012': 0.30; 'file': 0.32; 'good.': 0.32; 'comments': 0.33; 'traceback': 0.33; 'to:addr :python-list': 0.33; 'languages': 0.33; 'pm,': 0.35; 'something': 0.35; 'received:org': 0.36; 'two': 0.37; 'being': 0.37; 'far': 0.37; 'subject:: ': 0.38; 'skip:l 20': 0.38; 'shows': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'between': 0.63; 'url:blogspot': 0.64; 'here': 0.65; 'offer': 0.65; 'lack': 0.71; 'url:11': 0.71; 'subjectcharset:utf-8': 0.72; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: duck typing =?UTF-8?B?YXNzZXJ04oCP?= Date: Thu, 08 Nov 2012 15:39:24 -0500 References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: 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: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1352407186 news.xs4all.nl 6936 [2001:888:2000:d::a6]:43050 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32979 On 11/8/2012 12:34 PM, 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) > > The post below shows how programmer can assert duck typing between > two Python classes: > > http://mindref.blogspot.com/2012/11/python-duck-typing-assert.html > > Comments or suggestions are welcome. From the post: ''' So far so good. Let fix it and take a look at properties: from wheezy.core.introspection import looks class IFoo(object): def foo(self, a, b=None): pass @property def bar(self): pass class Foo(object): def foo(self, a, b=None): pass def bar(self): pass assert looks(Foo).like(IFoo) Here is output: test.py:21: UserWarning: 'bar': is not property. assert looks(Foo).like(IFoo) Traceback (most recent call last): File "test.py", line 21, in assert looks(Foo).like(IFoo) AssertionError ''' I view this check as an error. Properties are intended to be transparent to the user. One use of properties is to make something that is not a Mallard act like a Mallard. So this check breaks duck typing. -- Terry Jan Reedy