TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Underscore.py / a python port of underscore.js

37 pointsby serkanyersenover 12 years ago

14 comments

serkanyersenover 12 years ago
Hi Everyone,<p>First of all, thank you all for your comments. I have to clarify the purpose of this library. I'm originally a JavaScript developer and I didn't write a single line of python code before this project.<p>I needed a project to learn python and I choose something I'm already familiar with. Underscore.js has almost everything you want to learn on a new language and it thought me a lot.<p>When I completed the project I figured I can post it online and other clueless javascript developers like myself can learn python from a familiar place.<p>I'm fully aware that python already has most of the tools underscore has and this code is not pythonic at all. It was built that way intentionally. I wanted it to be the same as underscore.js.<p>Now I need your advice to write a better python code and maybe create a useful library after all.<p>Thank you all.<p>PS: I wrote this comment from mobile phone so please excuse any errors.
评论 #4495855 未加载
评论 #4495823 未加载
jmagnussonover 12 years ago
Why, oh why?<p>map(str.upper, ['foo', 'bar']) == _(["foo", "bar"]).invoke("upper")<p>[w for w in ["foo", "hello", "bar", "world"] if len(w) &#62; 3] == _.filter(["foo", "hello", "bar", "world"], lambda x, <i>a: len(x) &#62; 3)<p>sorted([i </i> 2 for i in [10, 48, 56, 30, 20] if i &#62; 20]) == _([10, 48, 56, 30, 20]).chain().filter(lambda x, <i>a: x &#62; 20).map(lambda x, </i>a: x * 2).sortBy().value()
评论 #4495613 未加载
评论 #4495593 未加载
michaelhoffmanover 12 years ago
I hesitate to add to the negative response to what is surely a well-meaning free project for the world. But `_` is really a poor choice for a variable name in Python. It is already used as:<p>* the value of the last expression in the REPL<p>* a conventional abbreviation for `gettext.gettext` (<a href="http://docs.python.org/library/gettext.html" rel="nofollow">http://docs.python.org/library/gettext.html</a>)<p>* a conventional throwaway variable (for `str.partition` and other functions that return a larger tuple than you want)<p>And for most of these features, invoking them through a object/class method rather than a standalone function is unwieldy (and unpythonic)
评论 #4496323 未加载
评论 #4496339 未加载
djacobsover 12 years ago
It is hilarious that someone would have the knowledge to build this but not know that it was already implemented as part of the language.<p>Is this a parody?
评论 #4495798 未加载
评论 #4496935 未加载
评论 #4495762 未加载
stephen_mcdover 12 years ago
Most of the methods provided are functions already built into Python (map, filter, reduce, all, any, min, max)<p>Also the underscore variable name conflicts with the common convention of using an underscore as the i18n translation function.<p>Those issues aside, the templating looks handy, and I'm sure the whole thing was fun to write.
FuzzyDunlopover 12 years ago
<p><pre><code> VERSION = "0.1.2" """ Version of the library """ </code></pre> This is a good example of unnecessary commenting.<p>But I think this port misses the point of Underscore.js, which is to make the same functional features of the language available to all browser implementations, because not all browsers have forEach, or filter, or reduce, etc. In Python, or other languages? The features are already there.
评论 #4495890 未加载
draegtunover 12 years ago
Other Underscore.js ports (that I know of):<p>Perl - <a href="http://vti.github.com/underscore-perl/" rel="nofollow">http://vti.github.com/underscore-perl/</a><p>Lua - <a href="http://mirven.github.com/underscore.lua/" rel="nofollow">http://mirven.github.com/underscore.lua/</a><p>PHP - <a href="http://brianhaveri.github.com/Underscore.php/" rel="nofollow">http://brianhaveri.github.com/Underscore.php/</a><p>Actionscript 3 - <a href="https://github.com/amacdougall/underscore.as" rel="nofollow">https://github.com/amacdougall/underscore.as</a>
borntypingover 12 years ago
There is absolutely no reason to use this. As far as I can tell, it's a direct port of underscore.js, and implements nothing but functionality that's already in python.
评论 #4495809 未加载
yoduhover 12 years ago
My opinion: Anytime you share code by posting it publicly, you should always be prepared for negative feedback. In some cases that might be the whole point of posting it: you want to learn from your mistakes.
why-elover 12 years ago
Good job! I know that some of these functions maybe have already occurred to Python developers (map et al), but this probably lead you to a broader understanding of underscore.js itself.
评论 #4495814 未加载
评论 #4495777 未加载
mark_storyover 12 years ago
While I can understand this might have been fun from a hacking/building experience, I don't understand why do it. Everything except the micro templating already exists in python.
victorlinover 12 years ago
This doesn't bring anything good to Python. Python can do those things nicely. I can't see any point of this library. Also, underscore already has its meaning in some places, such as for i18n. For example, _('Text to be localized'). Another use of underscore is to store the previous output in console. Although you can rename it, still, it's pointless.
sontekover 12 years ago
I think the biggest benefit of this is having the template language ported to python so you can use the same templates server side or client side.
评论 #4496271 未加载
cpyluaover 12 years ago
Collection functions are not that useful in Python. For Function functions I'd prefer using decorators.
评论 #4496316 未加载