Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

That's not true - Python implements sorted by calling `__lt__` on the object: https://docs.python.org/3/library/functions.html#sorted


__lt__ is just the way to overload the comparison operator, which of course is going to be called by the sort. What OP is saying is that there's no __sort__ or anything like that.


But that's not really true either - there's __len__ which len() calls: https://docs.python.org/3/reference/datamodel.html#object.__...

You can add iterators with __iter__, reversed() calls __reversed__, etc.


Sorted mandates the type you give it implements __iter__, aka that it is iterable. Ruby defines sort as a method on Enumerable.

The difference is that most likely that CPython and CRuby started with different capabilities to map native implementations into the language.

The biggest difference between the two is python adds more functions into the global namespace, while Ruby pushes pretty hard to encapsulate logic within types.


The discussion is about how to provide additional functionality.

To be sortable, you collection has to be a collection, and it has to have comparable elements. That has to be provided by the type itself.

The difference between Python and Ruby is how to add more functions. If you make a library to sort collections (or format phone numbers), the Ruby way is to monkey-patch collections (or numbers). Whereas the Python way is to offer a free function that takes an iterable (or number). This is what we are discussing.

Of course Python classes have methods.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: