Object type check

Calvin Spealman ironfroggy at gmail.com
Wed Feb 7 11:28:38 EST 2007


The answer is to do nothing at all. Use the interfaces of the objects
that you expect. Treat them like numbers if you expect them to be, or
stirngs, or iterables. Call methods and access attributes you expect
to be there. If the caller passes sometihng bad, and something doesn't
work, they'll find out about it and consult your documentation to see
what they did wrong. Dont restrict them to particular types. You would
not restrict them to a particular class in C#. Instead, you define the
interfaces simply by how you use the objects. This is called duck
typing (http://en.wikipedia.org/wiki/Duck_typing) which states "If it
walks like a duck and it quacks like a duck, it must be a duck." In
the end, isn't that what matters to you? Not the type or defined
interfaces of an object, but that it does what you expect.

On 7 Feb 2007 08:17:55 -0800, king kikapu <aboudouvas at panafonet.gr> wrote:
> Hi to all,
>
> in statically-types languages, let's say C# for example, we use
> polymorphism through interfaces. So we define an interface I with
> method M and then a class C that implements I interface and write code
> for the M method.
> So, if we have a function that takes a parameter of type I, we know
> before-hand that it will have an M method to call.
>
> But in dynamic languages this is not the case and we can pass whatever
> we want to that function. Assuming that someone writes a library in
> Python that other programmers will use, what is the correct way to
> check inside that function if the parameter passed is of the correct
> type, maybe "isinstance" BIF ?
>
> Thanks in advance!
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


-- 
Read my blog! I depend on your acceptance of my opinion! I am interesting!
http://ironfroggy-code.blogspot.com/



More information about the Python-list mailing list