6 ms·
I prefer the Python code here. The import is explicit, so if you want to know where "date" comes from, that's easy. In a modern IDE I don't write imports manual
by ouija 4y ago
I prefer the Python code here. The import is explicit, so if you want to know where "date" comes from, that's easy. In a modern IDE I don't write imports manually anyway.
You could write it as a one-liner in Python as well, but I would prefer not to.
print("It's Saturday") if date.today().weekday() == 5 else None
That the Python library doesn't define constants for days is unfortunate, but not a limitation of the language.
- petepete 4y agoWhy do I need to remember that Saturday is 5?
- greenpeas 4y agoS looks like 5 if you squint. not hard to remember
- d0mine 4y ago>>> import calendar >>> calendar.SATURDAY 5
- petepete 4y agoYeah, I'll take `Date#saturday?` thanks.
- quickthrower2 4y agoYou are allowed to define your own constants.
- squeaky-clean 4y ago¿Por qué necesito recordar que el sábado es "Saturday"?
- throwawaymaths 4y agoTampoco "if", "for", "while"...
- unixhero 4y agoPorque en ese caso no usas un IDE moderno.
- wiseowise 4y agoRest of the language doesn't bother you?
- shaky-carrousel 4y agoThe ruby oneliner looks way cleaner and more natural than the python one.
- rcfox 4y agoWould you ever use either in a real codebase though?
- xipho 4y agoAs opposed to a fake codebase? This sounds a bit like https://en.wikipedia.org/wiki/No_true_Scotsman https://en.wikipedia.org/wiki/No_true_Scotsman.
- orf 4y agoAs opposed to either a snippet in a REPL or a comment on hacker news.
- xipho 4y agoThe code is part of a standard, used-almost-everywhere library, `date`. Code in these libraries, from what I undersand, have a ton of thought behind them. It might follow that there were good real-life use-cases ready to use the code that influenced its presence in that library? Also: https://github.com/search?l=Ruby&q=Date.today.saturday%3F&type=Code https://github.com/search?l=Ruby&q=Date.today.saturday%3F&ty...
- orf 4y agoSo the answer to the original question was simply “yes”.
- mod 4y agoNo, not as opposed to a fake codebase. I think it's clear what they meant. One incredibly common alternative to a codebase is a script.
- by_the_bay 4y agoAgreed, in complicated Ruby packages with layers and layers of dependencies it quickly becomes a nightmare to identify which class comes from which import, where it’s defined, etc. Never an issue in python
- cutler 4y agoThe else None is redundant, though. If you want a one-liner in Python just ignore the indentation: if date.today().weekday() == 5: print("It's Saturday")
- dragonwriter 4y agoPreferring the Python import syntax doesn't preclude preferring the Ruby library design.