3 ms·
With all due respect, these might be good examples for people trying to learn algorithms and data structures, but it's not a good example of Pythonic code. Here
by santiagobasulto 10y ago
With all due respect, these might be good examples for people trying to learn algorithms and data structures, but it's not a good example of Pythonic code. Here's an example of one of the group projects in our course, that implements a "Pythonic" Linked List, relying on Iterators and Magic methods: https://github.com/rmotr-group-projects/pyp-w2-gw-linked-list/tree/solution https://github.com/rmotr-group-projects/pyp-w2-gw-linked-lis...
(switch to the branch master if you don't want to see the solution and want to give it a try by yourself).
In any case, if OP is the owner of the repo, good job compiling all that code together.
- pryelluw 10y agoStupid question: Interface.py follows a pattern Ive seen in Java but not in Python. Am I simply too much of a Python noob?
- orf 10y agoNo, it's not a particularly pythonic thing to do, but it's not uncommon to have a bass class with some methods that should be implemented by children and throw an exception if not. That's not quite an interface as Java has it though.
- pryelluw 10y agoOk, that's what I thought. Thanks for the reply.
- dahart 10y agoSo, the title does say these are examples of structure & algorithms, and doesn't claim to be "pythonic". But why do you feel like using iterators makes it more "pythonic" than not using iterators? Is your list implementation more concise or better expressed? Does it handle cases the article doesn't? The code in the repo looks pythonic to me. This one looks pythonic to me. http://stackoverflow.com/a/283630/1424242 http://stackoverflow.com/a/283630/1424242
- zimablue 10y agoAs someone who's written in a few languages and is now learning/writing Python full time, "pythonic" is starting to cause me a twitch of pain every time I read it. It seems to imply the Python has the final solution in how to write code and therefore everything should be written in the way that Python elders have decided. Main example: I don't really like list comprehensions. They're pretty much the same thing as map/filter but less understandable or composable, nevermind you fix (with your choice of brackets in multiple places) your choice of list vs generator vs "pushing source" (not sure the right word) which you wouldn't have to with transducers. http://stackoverflow.com/questions/952914/making-a-flat-list-out-of-list-of-lists-in-python http://stackoverflow.com/questions/952914/making-a-flat-list... I'd rather read code where someone wrote "flatten".
- terminalcommand 10y agoIMHO pythonic means that you adhere with the principle "there should be one --and preferably only one -- obvious way to do it". And that one way is set out by the BDFL Guido :). I have theory that, python was introduced out of frustration with Perl. As we all know, Perl had the principle of TIMTOWTDI. Another far more tangential and biased theory here: As Python imposes strict programming principles, it resembles Java in forcing the programmer to abide by the styles set out by the language designers. And maybe that's why schools prefer to teach Python instead of Ruby as a scripting language. PS: I have nothing against Java and I love Python. My theory is based upon Guido's insistence on not opening the language fully to functional programming. I'd like to see better lambdas, functional programming without itertools etc. But I know Guido has his reasons, pythonic code uses list comprehensions instead of FP.