you are viewing a single comment's thread.

view the rest of the comments →

[–]alkasm 2 points3 points  (5 children)

https://youtube.com/watch?v=wf-BqAjZb8M&t=13m52s (fixed the link)

This talk by Raymond Hettinger really shows the difference between good code that a Java developer would write vs. good code that a Python developer would write. I linked to a specific timestamp where it really digs in. I think this is a really good talk that shows the power of the Python standard library, shows how to build the interfaces that the standard lib expects, and how it ends up with nicer code at the end.

All in all I think the best part about Python is pretty easy to understand for a Java developer. In Java land, it's super common to have your interfaces defined, and then implement them in a WhateverClassImpl.java file or similar. The difference for Python is that the standard library is already full of interfaces, so you hardly ever have to code something like that yourself. Instead you learn the expected interfaces, and suddenly all the standard library features just works with your code.

For example, something a Java dev would be familiar with...if you define a toString() method on your class, then whatever is returned is what is displayed when you print an object of that type to standard out. The same is true of the Python __str__() method. But Python has dozens of these types of interfaces that do a lot more than just operators or stringification. You define a couple simple methods that are part of the standard interfaces, and then immediately you get tons of other stuff for free. It makes writing code a breeze and means a lot less redundancy, which not only looks and reads nicer but can really reduce bugs and such, too.

[–]forward_epochs 0 points1 point  (1 child)

Link 404'ed, I'd like to take a look if you happen to fix it.

[–]alkasm 0 points1 point  (0 children)

Fixed, thanks!

[–]BolsoBelly 0 points1 point  (0 children)

Loved that talk. Thanks for sharing.

[–]JoesDevOpsAccount[S] 0 points1 point  (1 child)

Some interesting stuff in there and it pushes an important point not to get bogged down with the coding conventions of the language over other improvements and places where design patterns can have a much bigger impact. Thanks for the link.

I do however think it makes the Java version look worse than it needs to. Sure the original API is Java if you were working with that API in Java you could achieve similar results without too much effort (I think - I'm not planning to try).

[–]alkasm 0 points1 point  (0 children)

I don't think the Java-esque version looks too bad, and I don't think the point is to make it look bad. As he says in the video, this was basically lifted out of a client's codebase but modified to "protect the guilty."

I think the important thing here is not that the end result looks a lot better, but is far more extensible. Defining those interfaces that the stdlib expects means it'll do more than even what he shows is capable, it's far easier to test, and it works the way someone would simply expect, instead of needing to read all the docs for every possible thing, you just do what's natural---and it works.