Interfaces vs Duck typing

86 views
Skip to first unread message

Miguel Casal Guillán

unread,
Jun 18, 2015, 8:56:58 AM6/18/15
to growing-object-o...@googlegroups.com
Hi everybody!

Lately I'm learning Python and I'm enjoying a lot of its readability and simplicity, besides its huge ecosystem of modules.

As you already know, Python is a dynamic language that lacks some of the "classical" features of the OOP, interfaces between them. On the other hand, GOOS examples are in Java and they make extensive use of interfaces.

How am I supposed to do TDD in Python? The obvious approach would be not to fight against the language's nature and rather than creating interfaces, define my objects' APIs directly into the tests. After all, no types are enforced during runtime or compilation to bytecode, and if some object implements the needed methods execution will be fine (duck typing).

What's your opinion about that?


Thank you.

Steven Solomon

unread,
Jun 18, 2015, 9:13:29 AM6/18/15
to growing-object-o...@googlegroups.com
Miguel,

It's definitely a mental shift working in a dynamic language. I currently work only in CoffeeScript and I had to initially get over the fact that their isn't a standard type system and fight the urge to do psuedo-type-checking by hand within methods. Here is a small WIP example of how I write tests in CoffeeScript. Notice that I don't check for types nor that methods exist. If the object passed in is missing part of the interface JS with throw a TypeError.


I hope this helps.

Thanks 
Steve

--

---
You received this message because you are subscribed to the Google Groups "Growing Object-Oriented Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriente...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Steven Solomon

unread,
Jun 18, 2015, 9:18:09 AM6/18/15
to growing-object-o...@googlegroups.com
One addendum to that being that parts of the interface that are missing can be scary when they aren't invoked immediately; when registering as a listener, you won't know that the listener doesn't have the proper interface until it is invoked.
 

Miguel Casal Guillán

unread,
Jun 18, 2015, 9:40:44 AM6/18/15
to growing-object-o...@googlegroups.com
Hi Steven,

I believe this is the way to go. Call the method on the object passed in, and if it doesn't know how to fulfill the call, handle the error gracefully.

Cheers.
To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriented-software+unsubscribe@googlegroups.com.

Josue Barbosa dos Santos

unread,
Jun 18, 2015, 9:49:34 AM6/18/15
to growing-object-o...@googlegroups.com

Well,

I do not program in dynamic language but if i will probably i would use something like interfaces to document my roles.

Mock roles, not objects. Remember?


To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriente...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to the Google Groups "Growing Object-Oriented Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriente...@googlegroups.com.

Steve Freeeman

unread,
Jun 18, 2015, 9:50:29 AM6/18/15
to growing-object-o...@googlegroups.com
Python is a favourite of mine.

Remember that much of the early TDD work was done in Smalltalk, also untyped but with excellent IDE support. One strong advantage is that there’s just fewer lines of code to consider.

You’ll need find your own way. Some of it you might be able to do with convention and a little documentation. Or you can define classes to use as interfaces (no leading ‘I’ please) as Python supports multiple inheritance. Or just keep your types small and pay attention. The most important thing is to be obsessively consistent.

There is an O’Reilly book on TDD with Python which I believe is decent (another member of my to-read stack).

S

Miguel Casal Guillán

unread,
Jun 18, 2015, 10:34:06 AM6/18/15
to growing-object-o...@googlegroups.com

Convention and documentation seem appropriate candidates for a language that claims to be more practical than purist, I guess :)

Others have recommended that book as worth reading.

Thx!
> To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriented-software+unsubscribe@googlegroups.com.

Caio Fernando Bertoldi Paes de Andrade

unread,
Jun 18, 2015, 11:55:34 AM6/18/15
to growing-object-o...@googlegroups.com
I have been working with Python lately and I substituted interfaces for data structures that are shared between the collaborators. Data structures represent the communication contract collaborators are bound to.

I have been fairly successful with this approach, but there’s only one challenge I am still struggling with:

Since it’s very hard to Find Usages in a dynamic language, I’m never sure a method or field can be safely removed. Having a test case covering them doesn’t prove a real caller is using them.

In a static language it’s easy to see if anyone else depends on that contract (interface), but in a dynamic language it’s quite hard.

Does anyone have any thoughts on that?

Caio

Steve Freeeman

unread,
Jun 18, 2015, 11:56:49 AM6/18/15
to growing-object-o...@googlegroups.com
Are you using a proper IDE, like Idea? It’s not entirely perfect and probably won’t catch reflection, but it covers most of those cases.

S

Nat Pryce

unread,
Jun 18, 2015, 12:52:46 PM6/18/15
to growing-object-o...@googlegroups.com
On 18 June 2015 at 14:50, Steve Freeeman <st...@m3p.co.uk> wrote:
Python is a favourite of mine.

Remember that much of the early TDD work was done in Smalltalk, also untyped but with excellent IDE support. One strong advantage is that there’s just fewer lines of code to consider.

Smalltalk also has an impressive mocking library, despite (or probably because of) not having explicit interfaces.  See http://www.slideshare.net/zeroflag/tdd-with-babymock-2 and https://www.youtube.com/watch?v=ZDjEky4u16o.

--Nat

 

You’ll need find your own way. Some of it you might be able to do with convention and a little documentation. Or you can define classes to use as interfaces (no leading ‘I’ please) as Python supports multiple inheritance. Or just keep your types small and pay attention. The most important thing is to be obsessively consistent.

There is an O’Reilly book on TDD with Python which I believe is decent (another member of my to-read stack).

S


> On 18 Jun 2015, at 13:56, Miguel Casal Guillán <d...@miguelcasal.net> wrote:
> Hi everybody!
>
> Lately I'm learning Python and I'm enjoying a lot of its readability and simplicity, besides its huge ecosystem of modules.
>
> As you already know, Python is a dynamic language that lacks some of the "classical" features of the OOP, interfaces between them. On the other hand, GOOS examples are in Java and they make extensive use of interfaces.
>
> How am I supposed to do TDD in Python? The obvious approach would be not to fight against the language's nature and rather than creating interfaces, define my objects' APIs directly into the tests. After all, no types are enforced during runtime or compilation to bytecode, and if some object implements the needed methods execution will be fine (duck typing).
>
> What's your opinion about that?
>
>
> Thank you.
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "Growing Object-Oriented Software" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriente...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--

---
You received this message because you are subscribed to the Google Groups "Growing Object-Oriented Software" group.
To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriente...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Caio Fernando Paes de Andrade

unread,
Jun 18, 2015, 8:56:29 PM6/18/15
to growing-object-o...@googlegroups.com
Yes, I'm using PyCharm. It gets hard because I write short, context dependant names (e.g. "title" or even "id"), and they exist in many contracts, so it's almost impossible for the IDE to find only the correct ones.

Caio

Steve Freeeman

unread,
Jun 22, 2015, 7:33:57 AM6/22/15
to growing-object-o...@googlegroups.com
I know PyCharm isn’t quite up to scratch. Surely “canonical” names like these should change very much, the question then becomes whether an object has one or not.

S

Matt Wynne

unread,
Jun 22, 2015, 6:09:16 PM6/22/15
to growing-object-o...@googlegroups.com
I also think the Sandi Metz book, Practical Object-Oriented Design in Ruby is really good on this. There’s not much about TDD (that’s assumed) - but there’s plenty about the importance of protocols in a dynamic language. She’s an ex-smalltalker.

To unsubscribe from this group and stop receiving emails from it, send an email to growing-object-oriente...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

cheers,

Reply all
Reply to author
Forward
0 new messages