Re: duck typing assert

Discussion in 'Python' started by Ian Kelly, Nov 8, 2012.

  1. Ian Kelly

    Ian Kelly Guest

    On Thu, Nov 8, 2012 at 10:30 AM, 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:
    >
    > mindref.blogspot.com/2012/11/python-duck-typing-assert.html
    >
    > Comments or suggestions are welcome.


    Overall, it looks potentially useful to me. Looking over the
    wheezy.core.introspection source, it appears to ignore special method
    names. For example, if you do:

    class IFoo(object):
    def __len__(self):
    pass

    class Foo(object):
    pass

    assert looks(Foo).like(IFoo)

    I would expect this to fail, but judging from the code it would not,
    as it ignores all names starting with '_'. That makes some sense for
    private names (but on the other hand, why would you declare private
    names in an interface unless you want to enforce their presence?), but
    names like __len__ should not be considered private.

    Another comment I have is on properties, and descriptors in general.
    Does this allow something declared as a property to be implemented as
    a simple class attribute, and vice versa? Or can something declared
    as a property be implemented with some custom descriptor class? I
    think the answer is "no" to both, as the check it performs is
    "t2.__class__ is not t.__class__". I assert though that in general
    the type of a descriptor (that is not a simple class attribute) is not
    as important in duck testing as its API -- and all descriptors have
    basically the same API of __get__, __set__, and __delete__.
    Ian Kelly, Nov 8, 2012
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duck Typing

    , Sep 16, 2003, in forum: Python
    Replies:
    10
    Views:
    680
    Steve Holden
    Sep 17, 2003
  2. Replies:
    15
    Views:
    559
    Isaac Gouy
    Sep 1, 2006
  3. Alex Vinokur

    assert(x) and '#define ASSERT(x) assert(x)'

    Alex Vinokur, Nov 25, 2004, in forum: C Programming
    Replies:
    5
    Views:
    908
    Keith Thompson
    Nov 25, 2004
  4. Replies:
    2
    Views:
    318
    Terry Reedy
    Jan 28, 2008
  5. Andriy Kornatskyy

    RE: duck typing assert

    Andriy Kornatskyy, Nov 8, 2012, in forum: Python
    Replies:
    0
    Views:
    178
    Andriy Kornatskyy
    Nov 8, 2012
Loading...

Share This Page