23 ms·
Why People Should Learn Python
- iagooar 10y agoNow that I have a chance to ask: I've always found the def __init__ method with 4 (!) underscores one of the ugliest things I've seen in a programming language. Don't get me wrong: I like Python and know it's truly good, but __why__??
- MerreM 10y agoI believe it's part of the language design, a phrase you'll find quite a lot in Python language discussions, especially is more esoteric features is; "We're all consenting adults". There are certain areas of the language which may not behave exactly the way you'd be expecting, if you're new to it. These usually have an unusual syntax, which will cause you to find out more. They're basically the mini-roundabouts of python. https://en.wikipedia.org/wiki/Roundabout#Mini-roundabouts https://en.wikipedia.org/wiki/Roundabout#Mini-roundabouts If you know what you're doing you cruise straight over, but if for any reason you're unsure, you're forced to find out more before continuing. It's not designed to stop you, because "We're all consenting adults", but that doesn't mean it's going to encourage you to do something that might be harmful.
- icc97 10y agoUnless of course you're in Belgium where all junctions are by default preference from the right (so like a mini-roundabout but with no markings), which are opposite preference from roundabouts here which are preference from the left. But some junctions are labelled, some aren't depending on whether the local council could be bothered, some people will forcefully use their preference and risk a crash but 90% will ignore it. Driving in Belgium - the scripting equivalent of batch files
- liaukovv 10y agoSo people don't call it directly Defense mechanism of sorts
- masklinn 10y agoAlso limits chances of conflict with user-defined code.
- deleted 10y ago[deleted]
- rdtsc 10y agoIt is a convention for protocols. There are others you see __len__, __eq__, __new__, __str__ and so on. It did seem odd at first (I learned, C, Java, C++) first. But then after a while saw how it fits in a consistent way in the language. Make many things simpler, fits with duck-typing, fits with how classes are constructed, and so on. Visually it stands out right away, you so you know by looking these are special methods (say instead of being called "init", "eq" or "new") Felt the same way about "self" argument. But then now I really like it and see it as a very nice idea.
- deleted 10y ago[deleted]
- kozhevnikov 10y ago"The use of double underscores to indicate "magic" words goes at least as far back as the C preprocessor, which used that convention starting in the 1970s (e.g., __FILE__, __LINE__). It did so because there were no symbols that were off-limits to the preprocessor, so a rarely-used naming pattern (multiple underscores and upper case words) virtually ensured a lack of conflicts." http://programmers.stackexchange.com/a/228340/84451 http://programmers.stackexchange.com/a/228340/84451
- pmelendez 10y agoThere was a technical reason for C though, the preprocessor is just a bit more than an automatic text replace, in Python it seems it was just a design decision. Also in C those where not words embedded in the language, in python case it is.
- zubspace 10y agoCall me ignorant, but I agree. Having something like __init__ and __new__ is very confusing for a beginner, along with stuff like "__init__.py". It doesn't stop there: Take something like __str__ and __repr__ combined with str(), repr(), vars(), dir(), print(), pprint.pprint()... I have no idea how anyone ever thought it was a good idea to have that many ways to output the content of a variable. Even after all the time I used python I never managed to find a consistent way to output variable values. I generally like python as a scripting language. But stuff like this made me constantly lookup obscure details in the documentation which made it very hard for me to completely embrace the language. It also prevented me from diving into more arcane stuff like meta-programming...
- dismantlethesun 10y ago> combined with str(), repr(), vars(), dir(), print(), pprint.pprint() Very true! Though since in practice, most people simply use a graphical debugger or pick one of the above as their champion (pprint.pprint is my personal favourite), then I doubt this situation will ever be changed.
- amyjess 10y agoThe dunder functions are there to provide an interface to global functions and operators. If you want to affect how str(x) behaves, you override x's __str__() method. Operators behave this way too: for example, if you want to overload the == operator for a class, you override __eq__() in that class (for any Java developers reading: Python == works like Java equals(); the Python version of Java == is the is operator, which cannot be overloaded). Yes, that means x == y is just syntactic sugar for x.__eq__(y). The dunders are there so you have the freedom to name your methods what you want without having to worry that you'll accidentally clobber a method that affects a global or an operator, as most people won't just up and decide to both begin and end a method's name with __.
- pmontra 10y agoAgreed. That, the explicit self argument (which no other major OO language needed) and the : at the end of lines where you would need a { in other languages. If you google you find that there are good reasons for all of those choices but IMHO they are wrong solutions to the problem they solve and make Python look ugly. At least in 2016 they could make the colon optional. The colon is so ironic for a language that takes pride in replacing braces with indentation. It's there because of readability, see https://docs.python.org/3/faq/design.html#why-are-colons-required-for-the-if-while-def-class-statements https://docs.python.org/3/faq/design.html#why-are-colons-req... but so, how about an almost Erlang-like full stop? if condition: statement statement. That would make semantic indentation optional, IDEs and editors know how to reindent code, spare us with bugs introduced by careless copy and paste. Nevertheless, Python has been widely successful and it's here to stay for a long while. I hope designers will look at those bizarre features and think of better solutions for future languages. Remember that Python has its roots in the 80s. By the standards of the time it was very good. Compare it with the order of magnitudes worse design of PHP, from middle 90s, and there is a lot to forgive to Python. Maybe Guido Van Rossum would make different choices if he were to design his language now, under the influence of the languages of the last 20 years.
- guitarbill 10y ago> how about an almost Erlang-like full stop? This is just brackets/braces in disguise, at which point you might as well follow convention and use {}. This is obviously a very personal issue, but for me, the whitespace has less cognitive load. E.g.: in languages with brackets, people usually still indent the code for readability. I've also found that when teaching people to program, consistency works well. I think this is one of the reasons Python is easy to pick up, just because Python code consistently looks like Python.
- pmontra 10y agoFor those languages is not people who reindent code but it's the editor or the IDE. There is usually a key to force reindentation. That is possible because {} or even the end in Ruby are easy markers for the block (there are multiple markers for a block start in Ruby). Is there any automatical indentation function in some editor for Python? If not, it's Python the unfortunate language where people has to indent code to feed the compiler. I always hated doing the compiler's job. I'm asking because I'm using Python little and only for short scripts, so I didn't bother investigating much. Still I've been bitten a couple of times by bugs introduced by moving code around and not noticing that a line was not indented correctly. That in Python and in Haml (http://haml.info/ http://haml.info/) If somebody knows about those tools I will appreciate and it will make my life easier. BTW, I'm fixing somebody's else Python scripts right now :-)
- perlgeek 10y agoThe second annoyance is that you need to write __init__ methods quite often. Correct me if I'm wrong, but there doesn't seem to be built-in support in Python for having attributes initialized by the the constructor. For example in Perl 6, I can write class Point { has $.x; has $.y; } ... and I get a constructor Pair.new(x => 1, y => 42) for free, no need to write custom initializers.
- squeaky-clean 10y agoNothing built in, and generally not recommended, but you can do it! If you want to be really flexible and just allow any attribute, you can do this: class Foo: def __init__(self, *args, **kwargs): self.__dict__.update(**kwargs) And it will automatically assign any keyword arguments you use as attributes to the object. For example `foo = Foo(name='Bob', age=99)` If you still want to keep a strict list of allowed attributes, you can define them as parameters, and use a shortcut to assign all local variables to attributes. class Foo: def __init__(self, name, age): self.__dict__.update(locals()) del self.self So `foo = Foo(name='Bob', age=99)` will still work as will `foo = Foo('Bob', 99)`. But `foo = Foo('Bob', 99, True)` will throw an error, as will `foo = Foo(name='Bob', age=99, likes_cake=True)`. You can add kwargs back to the parameter list if you want to allow assigning any attribute. This isn't recommended though. So for all practical purposes, Python does require a bit of boilerplate in the constructor. Edit: Realized a cleaner way to do the second example.
- amyjess 10y agoI believe you could also accomplish this with a metaclass, as well.
- mmariani 10y agoJust import attrs [0] and don't worry about those annoying dunder methods. [0] https://pypi.python.org/pypi/attrs/16.0.0 https://pypi.python.org/pypi/attrs/16.0.0
- wibr 10y agoI first learned Python and then much later C, and for me at least learning Python first was very motivating because you can get things done so easily, due to the built-in batteries and the wealth of external libraries. Seeing that the computer needs less than a second for what would have taken me many hours of tedious work was pretty eye opening. Learning C later on helped me understand some basic concepts better and being able to speed up Python e.g. using Cython can come in quite handy.
- berntb 10y agoWhy is a beginner's introduction to one of the common scripting languages at the top of HN? Most everyone here should know at least one of the usual suspects (Ruby, Perl, Python, etc). They are all quite similar in capabilities. Is introduction to loops at the top, in an hour? Edit: The few newbies here which don't know a scripting language are hardly the ones voting this up... Edit 2: BanzaiTokyo is free to explain the glaring superiority of one of the scripting languages over others...? :-) Edit 3: erelde, so you really think a beginner's guide to a scripting language "gratifies [HN reader's] intellectual curiosity" enough to be top of HN...?
- jaredsohn 10y ago>Why is a beginner's introduction to one of the common scripting languages at the top of HN? I think there might be more non-programmers here than you think. Also, those who do program may have friends who don't and they may find the article interesting as something they can refer those friends to.
- BanzaiTokyo 10y agoPython is very and increasingly popular and I guess the reason it is at the top is to recommend to the people who practice Perl or Ruby to take a look at this language as well.
- PerfectElement 10y agoI've been programming for 20 years and don't know any of those languages.
- wiz21c 10y agoThat's surprising! So which are the languages you use (I'd expect to not know any of them :-))
- PerfectElement 10y agoC++, C#, Javascript and now Swift. I know many others who have been programming their whole lives in one or two programming languages. Once you get good at something and are well compensated, there may not be enough motivation to learn new languages. Specially when you have kids and other hobbies.
- clumsysmurf 10y agoA new & free introduction to Python from Jake VanderPlas can be gotten here: http://www.oreilly.com/programming/free/a-whirlwind-tour-of-python.csp http://www.oreilly.com/programming/free/a-whirlwind-tour-of-... I was looking for a dense & concise guide to the language, but one of the most popular books "Learning Python 5e" by Lutz was too long @ 1600 pages.
- thewhitetulip 10y agoHis book is awesome! I wish I had it when I started Python. I had gone through the documentation tutorial thrice, the entire documentation once or twice. Started writing code and then understood python. I found that learning Python is simple, mastering it is difficult, the magic which happens all the times makes it difficult to understand, which is the reason why static languages are awesome as less magic as possible.
- deleted 10y ago[deleted]
- atemerev 10y agoThe person who wrote this obviously has "the hacker's mindset". He praises expressivity and malleability of the language. Well, in this case it might be advisable to take a look at Perl. And never go back. Perl (especially Perl6) is hacker's paradise, where everything's true and nothing is forbidden. As somebody who shares the same mindset, I truly enjoyed my years with Perl, and I occasionally return to this experience.
- deleted 10y ago[deleted]
- viach 10y ago> is hacker's paradise And support engineer's hell
- tgfeathers 10y agoSo is Python. I've worked on code bases in several languages, and the greatest horrors I've seen are in Python and C++, in that order. Python is a strange case. The CPython code base seems to be quite well organized and well written, and about 10% of packages on GitHub or PyPI are extremely solid. The problem is the staggering amount of packages (public or in-house) that have 100000+ LOC and use the worst features of OO, functional, and procedural programming at the same time. The result is not spaghetti code, but ravioli code where there's zero control flow and "everything happens somewhere else". Add to that the "consenting adult" philosophy, and you have the modern equivalent of goto mixed with self-modifying code. I'm sure Perl has all that, but it's far from the only language.
- greenspot 10y agoIs it just my feeling or are there languages which are preferred on HN? Indicators for languages which are liked by the HN community: - Positive stories get many upvotes and are instantly at the top of HN - Most comments are positive - Frequent coverage on HN about the language Indicators for languages which are not liked by the HN community: - Rants get many upvotes and are instantly at the top of HN - Most comments are negative about the language - Rare coverage on HN Applying those indicators, following languages should have these like-levels (on a range from 1 to 5): Python ***** Go **** Node.js * C#/.NET *** Java *** RoR ** Ruby *** Elixir *** Haskell **** Closure *** C **** Rust ***** C++ *** PHP * Swift **** This is just my feeling, happy to hear your perception. And this does not reflect my opinion about those languages in any way.
- AhtiK 10y agoNode.js is a JavaScript runtime, not a language. Used for server-side JavaScript and web frontend build pipeline. Replacing Node.js with JavaScript doesn't help either as the frontend vs server-side vs npm ecosystem vs es2015 becomes a loaded question .)
- funkyy 10y agoBut you writing this as a bad thing? It's normal for a community to pick tools/solutions that are preferable. A lot of HN users are for example startup owners/creators so it's normal that they will prefer easy and semi-automated language like Python.
- pawadu 10y agoIt would be interesting to hear from people maintaining code that came from these startups. I suspect they would have a very very different view of things :) Maybe something investors should think about? Ehh, who am I kidding...
- funkyy 10y agoThis simple code and more time on hands to spend on promoting = successful startup. No one really cares about scaling YT-like app when it has 10 users. Developers will handle that. And if they want jobs, startups must be made. As you might know, successful startups are made by less technical people mostly...
- hacker_9 10y agoWhy you shouldn't learn Python: 1. In-consistant syntax. 2. The language uses exceptions to control flow of logic. 3. Divided community, since Python 3+ included breaking changes to the standard. 4. Un-discoverable APIs. You better hope the documentation is bulletproof else the API could change in any which way during runtime. 5. Poor error messages. If an import goes wrong you are not told why, and so on. 6. Ultimately slow performance for anything marginally complex. 7. Poor tooling.
- lcnmrn 10y agoName a single language that covers all those things and has a pretty syntax.
- ben_jones 10y ago01000010 01101001 01101110 01100001 01110010 01111001 00111111 00001101 00001010?
- dpatru 10y agoBinary?
- omginternets 10y ago1
- jeffwass 10y agoYou had me until the CR LF, but clearly the true hacker's choice is just the LF. <ducks>
- dalke 10y ago10010 10202 11020 11002 10121 11020 11111 01020 00101
- enrmarc 10y agoIMHO Ruby is a good candidate.
- joshvm 10y agoAlways fun writing compact Python: import sys from collections import Counter cnt = Counter([n.strip() for n in sys.stdin.readlines()]) for key, val in cnt.most_common(): print(val, key) This isn't just playing code golf, it goes with point (2) - awesome libraries. One of the joys and curses with Python is finding the Pythonic ways to do things that you've hamfistedly written your own way for months.
- aidos 10y agoAnd you can actually ditch the list comprehension and put it directly in the function call! Counter(x for x in y)
- masklinn 10y agoFWIW the Counter line can be simplified slightly: cnt = Counter(n.strip() for n in sys.stdin) * no need for a list comprehension, a generator expression will do * file objects are iterable, and iterated line-wise
- probably_wrong 10y agoFor all its virtues, I feel introductions to Python tiptoe around a very important topic: yes, Python is nice and easy, but you'll have to program the Python way too. Try running a nested loop on a non-trivial example, and you can end up spending minutes in what would take milliseconds in any other language. If you want to program in Python, you must get used to the functional paradigm. Not "should", "must". Now, I'm all in for bringing more functional programming into daily life, but I find a bit dishonest that beginners are not told about this. My first serious program took about a week for a task, and shaving that time down involved a lot of pain changing simple nested loops into multiplications of Numpy matrices. There was a lot of caching too. I have no idea how someone without a CS degree would have dealt with this.
- technomancy 10y agoWeird, I thought that Python was trying to discourage functional programming with its extremely limited lambdas and the proposal to move map and reduce out of the core language.
- brianwawok 10y agoKinda. Many things you can do with a map can also be done with a list comprehension or generator, so that seems to be the way to go. Moving from Scala to Python it was annoying at first.. but honestly the code is easier to read in most cases, so no complaints here.
- LyndsySimon 10y agoIIRC, the limitations on lambdas has to do with the way whitespace is parsed rather than being an explicitly conscious decision.
- andybak 10y agoGuido also maintains that by the time you need multiline lambda's, the extra cost of giving the function a name is more than outweighed by the improved readability. I kinda agree. Certainly it guards against some of the code you see in overly nested javascript. The downside is that the code is slightly removed from it's call site - but rarely by much. Overall the increased complexity it would bring to the language has not been justified by anyone arguing for it's benefits. Giving the function a name isn't the worst thing in the world.
- agounaris 10y agoOne my feeling about python is that is a great language for "reference". Probably everybody and write and read it and its already good enough for general programming. There are edges where other languages are better but still is a great language to establish communication among different types of developers
- sjellis 10y agoYes, I've seen Python described as "everybody's second-favourite language". It's not the best at anything, but if your main requirement is a bunch of different people all need to be able to read and edit the same code then it's a good choice.
- enrmarc 10y agoCouldn't we say the same thing about Ruby?
- dagw 10y agoNot really. Ruby isn't anywhere near as widespread or well supported in fields like finance, math, various scientific fields and GIS. Python on the other hand has at least has pretty good support in just about any domain you happen to stumble into.
- dalke 10y agoAs a historical note, this phrase was originally applied to WordStar. Quoting Jim Mischel at http://www.mischel.com/diary/2005/08/22.htm http://www.mischel.com/diary/2005/08/22.htm : > While I'm on the subject of WordStar, I've heard a story that I haven't been able to verify. When Phillipe Kahn was asked why he chose to use the WordStar command set in the first version of Sidekick, he said that he asked a lot of people for their editor preferences. Almost everybody had a different first preference (back then it could have been Emacs, vi, Word Perfect, WordStar, Brief, Leading Edge Word Processor, or who knows what else). But almost everybody he asked knew WordStar and named it as their second preference. I don't know if it's true, but it smacks of truth. Certainly every microcomputer programmer I knew back in the late 80s was proficient with WordStar.
- kuma4 10y agoIn PowerShell, PS> Import-Csv .\names.txt -Header name | group name | select name, count Name Count ---- ----- cat 3 dog 2 mouse 1 bird 1
- latkin 10y agoSeriously, the author seems to have fairly limited knowledge of powershell. A 13-line python script is presented, followed by a smug "you can almost hear the UNIX folks complain about how verbose the PowerShell version is". First off, the provided Unix commands _don't work_ (should have used `sort -rn -k 2`), while the provided powershell, as verbose as it is, does work. Second, the entire python script along with the wrapping unix commands is encapsulated with a powershell 1-liner: cat ./names.txt | group | sort -d Count
- K0nserv 10y agoI switched to writing python as my main backend/scripting language with my new job. Previously I was using Ruby for most of this stuff with some Go. I must say that so far Python has been a vastly inferior experience for me. The languages are fairly similar all though I prefer the more talkative/english style of ruby and the use of functions over list and dict comprehension. The thing that really stands out to me though is the poor state of python tooling and the library ecosystem. Having used Rubygems and bundler pip feels like taking an enormous step back. It's much less expressive. It doesn't handle the difference between production only dependencies, regular dependencies and development only dependencies in a good way. It's difficult to differentiate between locked dependencies vs desired dependencies(the Gemfile/Gemfile.lock distinction). PyPi and especially using private PyPi registers is more complex than it is in Ruby. There seems to be fewer nice libraries and they seem to be spread across the web whereas Ruby centralises around GitHub. I also find that Python libraries have lacking or hard to find documentation in many cases. I wonder how much of this is due to the community spending a lot of time on the unproductive Python2/Python3 debacle instead of moving the language and the ecosystem forward. It seems like Ruby and Python would be in more similar positions had it not been for the problems around the Python 2/Python 3 upgrade. Ruby had the risk of a similar problems in the 1.8 to 1.9 upgrade, but it seems to have worked out better than the Python 2/Python 3 upgrade.
- korijn 10y agoCheck out conda sometime.
- K0nserv 10y agoThat looks interesting, I've also tried out pip-tools[0] which is really nice. However nice these tools are it seems to a bit at odds with the Zen of Python. Ideally there should be just one single good way to deal with packages. Ironically this is the case for Ruby, but not at all for Python. Ultimately you can always use some alternative method that solves it better, but you then have to take on the burden of convincing your co-contributors that it is a better idea than using pip and you also have to explain it to anyone that joins your project. 0: https://github.com/nvie/pip-tools https://github.com/nvie/pip-tools
- myst 10y agoIs it 2006 again?
- paistropical 10y agoIf you want to learn more in-depth Python: https://pythonspot.com https://pythonspot.com
- josteink 10y agoI just went with the "Fluent Python" O'Reilly book. http://shop.oreilly.com/product/0636920032519.do http://shop.oreilly.com/product/0636920032519.do It's a bit pricey, but I managed to score it on a 50% sale, and I have to say I think it's a very good book for people already fluent in other programming languages.
- eddd 10y agoI use python on daily basis for 5 years now. It's a really cool language, but: * Packaging is horrible * Releasing python code is a non-standarised nightmare. Every solution has it's own flaws * Big and complex projects in python are really hard to reason about * Poor support for concurrency (fixed in py3) Don't get me wrong, python is really cool as a proof-of-concept scripting language. But for mature and complex stuff, I'd recommend something with type checking and better support for release process.
- crdoconnor 10y agoI'm often frustrated with python packaging but after trying a few other languages' equivalents' (npm, go, etc.) I'm starting to think it's really not so bad.
- passiveincomelg 10y agoJust yesterday I had a frustrating issue. Digital Ocean VM with Ubuntu 16.04 and pip 8.1.1 installed via apt. "pip install -r requirements.txt" with pinned (recent) versions failed. I think it triggered this issue https://github.com/pypa/pip/issues/3687 https://github.com/pypa/pip/issues/3687. Didn't investigate though, since this was just a side project and it was 11pm. Made it work somehow. :-/
- overcast 10y agoI'm curious as to what issue you're running into with npm? npm install pug --save , installs to your local project, and adds to package.json. Not sure how that can get any easier! Makes deployments silly simple as well, and the ecosystem is massive surrounding it.
- crdoconnor 10y ago* I sometimes had to run "npm i" twice in order to get it to work. * It would often fail randomly. * node_modules has a fixed location. * There is a ludicrous number of dependencies even for the simplest projects. ~1000 is not unusual. Reasoning about that dependency tree simply isn't possible. * There was even a package to detect if a number was negative. It had a version 2 because there was a bug! * In general even small projects took an age to build. I'm sure this isn't all the craziness, it's just the stuff I ran into during my (fairly limited) exposure to npm packaging.
- technomancy 10y ago> Another thing is strict typing and debugging: since Python is an interpreted language, finding bugs wasn’t as easy Wait, what? How is its interpreter remotely relevant to its semantics? > if you have a syntax error in C, the program will simply not compile, on the other hand, in interpreted languages, the problem might go unnoticed for quite some time Hang on--Python allows you to run code that has syntax errors? I'm so confused.
- forkerenok 10y agoTo my knowledge syntax within a file is completely checked when that file is loaded. I think the OP referred more to errors in method and other reference names.
- michaelt 10y agoIf you write this: #!/usr/bin/python import time time.sleep(5) fdafadsfadsf It'll take 5 seconds to crash with a "NameError: name 'fdafadsfadsf' is not defined". Likewise, if you do this: #!/usr/bin/python import time x = 1 time.sleep(5) print "x is "+x It'll take 5 seconds to crash with "TypeError: cannot concatenate 'str' and 'int' objects" In a language like Java, these would be syntax errors, and they'd be picked up at compile time as they're illegal in Java's syntax. I assume they're legal in Python's syntax, because for all the syntax checker knows, time.sleep() might define fdafadsfadsf and convert x into a string. So they're runtime errors in Python. Of course, Java still has runtime exceptions - null pointer exceptions, for example.
- jasonpeacock 10y agoJava and Python are apples and oranges - one is a compiled language, the other is interpreted. Better to look at other interpreted languages like Ruby & Perl...and they have the same issue. It's a side-effect of using an interpreted language, but it also allows such languages to be self-modifying which can be very powerful when you need it.
- 10y ago
- douche 10y agoMy initial progression of languages was: QBasic, Visual Basic, C++, Java (1.5), Python. Python was leaps and bounds more fun and productive than flailing away with any of those. But today, I'm less excited about it. I've gotten spoiled by C#, that has static typing, and consequently, better tooling, as well as a good measure of the flexibility of Python, through dynamics, lambdas, LINQ. It seems Python is finally getting over the 2.X -> 3.X schism, but that kind of threw a wrench into things for a number of years, as well.
- danso 10y agoI was a long time Rubyist but switched over to Python because even though its characteristics and philosophy made me reflexively shirk it at first -- particularly significant whitespace, one-line-lambdas, and the clusterfuck of 2.x vs 3.x -- it seemed to me to be an obviously easier language to teach, nevermind its significantly stronger libraries for numerical work. I'm glad I made that decision. The language was easy enough to pick up in my spare time. I hardly ever use pandas/numpy unless I'm doing some really serious statistical work; the core language and patterns in Python are now pleasant enough for me to use in day-to-day data wrangling and hacking. I'm now mixing in Ruby for a current project -- mostly because I like Rakefiles, and also, Ruby's static site generators like Middleman -- and it's been surprisingly painful to do things in Ruby. I used to hate how when doing a Google search for something in the Python stdlib would bring up 2.x before 3.x...but it almost seems even more problematic in Ruby, where 1.9.3 and 2.0.0 docs keep coming up, even though official maintenance of them has been dropped. (2.0's maintenance ended in Feb. 2016 http://ruby-doc.org/core-2.0.0/Array.html http://ruby-doc.org/core-2.0.0/Array.html) If I didn't know better, I wouldn't even know what Ruby's official version was. For me, Google results show no other documentation for Ruby's Array, as the next results are from tutorialspoint and sitepoint. And that's not even getting into the issue of how problematic the do-things-how-you-like freedom of Ruby is incredibly hard to discern as an outsider. To give one example I recently had: how do you iterate through a human-calendar-based time interval, such as a month (i.e. any interval that doesn't have a constant length in seconds)? In Python, I remember how frustrating it was to figure out what the hell the difference was between datetime, time, calendar, the gmtime function, and then timedelta, and then the third-party libraries of pytz and python-dateutil. But once I understood them, which was largely a matter of realizing how naive I was about the nature of measuring time, doing something like iterating over calendar intervals is straightforward. This is my first Google result for "python iterate month intervals" -- it's from 2008 and still works perfectly: http://stackoverflow.com/questions/153584/how-to-iterate-over-a-timespan-after-days-hours-weeks-and-months-in-python http://stackoverflow.com/questions/153584/how-to-iterate-ove... Now here's the result for Ruby; all of the answers, as far as I can tell, are not only not-very-Rubyish, but not reliable either: http://stackoverflow.com/questions/1724639/iterate-every-month-with-date-objects http://stackoverflow.com/questions/1724639/iterate-every-mon... FWIW, I remember solving this problem by using the DateTimein ActiveSupport. Which is a credit to how much fun and joy it is to build things in Rails right out of the box. But that complexity catches up quick, and including ActiveSupport in day-to-day Ruby work can sometimes to unwanted monkeypatching.
- ekzy 10y agoI'm learning python just to be able to use tensorflow. I wish there was an interface in Ruby
- enriquto 10y agoThe "namecount" example is rather silly though. It can be solved by a shell script much shorter than one line (sort|uniq -c).
- iluxonchik 10y agoHey there! I've answered a similar observation in the comment section of the blog :) > Of course it can be done all with command-line tools, but the point was not to show you how you can count and sort word occurrences. I just wanted to show that it's possible to include Python scripts in your "command line workflow". I wanted to give a simple example that would give you a taste of the idea :)
- cdnsteve 10y agoPython is in space [1]. There is such a large breadth of fields that it has been successful in. Web apps, data analysis, machine learning, micro controllers, etc. It has a long track record for being stable, reliable and secure. The popularity over the years has been steady and with the recent addition of data science field and machine learning, it's seeing another growth spike. In my opinion, it's a highly versatile and very productive language that seems to be able to handle just about any job or industry you can throw at it. I personally switched to it full time and love it. [1] https://www.youtube.com/watch?v=Zm08hXeuv-I https://www.youtube.com/watch?v=Zm08hXeuv-I
- kozikow 10y agoAddressing some criticism from various comments: > Python 2 vs 3 It is mostly over. All important libraries are accessible on 3. It is clear that if you are starting from scratch you should go with 3. There are good tools to help with the migration (e.g. six). Some libraries start dropping python 2 support. > Bad tooling Having used Emacs with jedi for a while I switched to PyCharm to leverage optional typing. PyCharm beats my tooling experience in all languages I used expect Java: - Auto imports work well. - Code completion works very well, especially on optionally typed code. - Even refactoring works very well, failing short only to my experience with Java, but beating C++ or even Scala. - Debugging is actually a highlight - I can drop into full fledged REPL at break point. "Evaluate expression" in C++ or Java didn't come close. Profiling is indeed not as advanced as what I was used to on the JVM, but IMO it is sufficient for 95% of use cases. For that 5% it is clear from the start that you shouldn't go with python. Language specific performance monitoring in production becomes less of an issue when you start running inside containers and it starts to make more sense to monitor containers rather than individual processes. > Maintainability at scale I am using Python 3.5 optional typing and I feel my project is much more maintainable as a result. Among others, refactoring in PyCharm works very well. > Packaging and deployment As mentioned in another comment, personally I moved away from relying on language specific tools to do more than just "install version X of library Y" and manage my deployment and dependencies using docker. It have it's own problems, but so far it works very well, and it's quickly improving. > Performance In domain of scientific computing by using correct libraries and things like Cython or Numba you can come close to C++ performance levels. In other domains it's becoming more common to be disk/network bound, and then it doesn't matter if you use Assembler or Python. Also JITs are coming to Python: https://lwn.net/Articles/691070/ https://lwn.net/Articles/691070/. > Syntax/libraries/language features/etc. It is the most subjective section, but my experience with python in this regard is highly superior to Scala/Java/C++/JS.
- nrjames 10y agoThe long history of Python support in the scientific community has produced a rich catalog of domain-specific analysis libraries. Warts and all, it's simply not possible, in most languages, to achieve the analytical productivity you get when working in Python.
- hiram112 10y agoI've had to deal with Python for a long time. I even wrote my senior research project with it in college. But since then, I've never really had a need to use it, and I don't even feel there is a good reason for it. If you're writing something more than a quick shell script or a project that will last more than a few days, then there a dozen static languages that have better libraries, more powerful IDEs, etc. And misspelling a variable or missing a comma is not going to cause all sorts of problems. On the other hand, if you do need a quick one-off script or some duct tape, things like Perl or AWK, sed, and friends seem like a better bet. Python (and Ruby) seem bloated for quick scripts, and wholly lacking for larger projects.
- interdrift 10y agoAs the author mentioned Python is good for small stuff. If you are building a huge system where the jobs are spread between a dozen of people things could easily get out of control and you could end up in the dirt.
- thearn4 10y agoDo you feel this way due to the dynamically typed nature of the language (and the possibility of having ambiguous interfaces)? I've had pretty good luck with rather large scale python projects, though we've been pretty strict about working within a framework.
- interdrift 10y agoYeah mainly due to that. If you don't have a VERY strict approach to writing code, one wrong decision could turn into a few days of debugging..
- jasonpeacock 10y agoWhere 'strict' == 'unit tested', then I agree with you. You don't need strong typing to make good code.
- thearn4 10y agoI would agree that complete and meaningful unit and integration tests are definitely a necessity for a large Python projects. Or really for any software project meant to be maintained.
- jasonpeacock 10y agoYou can write crappy code in any language. I have seen large, easily maintained systems written in Perl, and I have seen horrible little tools written in Java. There's nothing specific to Python.
- jokoon 10y agoPython is so easy to write, use and read, I would not understand how a programmer would not know it already. It literally has everything you want in a language. Granted, it's not on-the-metal fast, and it still has some gimmicks, like variables behaving like pointers or references (which can be a little awkward), but still, python is the language of the decade.
- retro64 10y agoUgh. I tell you, I keep trying to pick it up because I hear so much about it. However, when I code in it I feel completely uninspired. I can only get so far into a book or tutorial before my eyes glaze over and I'm thinking about something else. I think when I code, I want to own the machine, not just skate on the surface. Python doesn't even crank my engine the way BASIC did years ago. I also think part of my problem is it doesn't solve any needs right now.
- jokoon 10y agoWell python is not for solving hard problems. If you want to own the machine you need C++ or C. Python is a language that makes programming easy. That's a lot.
- _c_ 10y agoWhy do programmers try to convince others which programming language to use? Why should they care what languages others are using?
- jorge-fundido 10y agoThe more popular your preferred tooling, the more opportunities in the job market.
- lllorddino 10y agoI hate Python. My linux distro comes with version 2.7 yet I should be using version 3?? Yes it makes writing complex programs quickly with its endless libraries, but I don't consider myself learning anything if all I'm doing is copy pasting the API docs. I've recently switched to Go and the language is small and easy, docs/source code are well commented, and lastly I'm actually learning how computer programs work.
- jasonpeacock 10y agoMost ppl are trying to solve problems/get work done, not learn how to re-implement functionality already available in libraries. Copy & pasting the API docs lets you focus on solving much harder & more interesting problems. If you want to learn how computer programs work, you don't have to use existing libraries - you can still implement the functionality yourself. By your argument (I hate python...it has libraries) any modern & moderately successful language with a large library ecosystem is bad, and you prefer Go precisely because it lacks those libraries?
- ferrari8608 10y agoI just wanted to point out a bit of syntax from the article. I'm a huge fan of compact variable declaration if statements. The code from the article: if name in names: names[name] += 1 else: names[name] = 1 That can actually be written as just one neat, readable line: names[name] = names[name] + 1 if name in names else 1 It may not be as readable to some who are used to spelling it out as an if/else block, but I really prefer the one line way. It reads closer to regular English I think.
- jasonpeacock 10y agoNO. That is not more readable, and is warned against in most style guides and code-quality books. In the original, at a glance I know exactly what is going on. In your version, I have to read the whole sentence carefully to notice that it's even a conditional and not a normal assignment, and then I have to mentally unpack it to understand the logic that you're trying to implement. If/else should never be a one-liner. Good code is boring code :)
- avyfain 10y agoYup, but the right way to do it would be a defaultdict, or a Counter anyway. from collections import defaultdict def count_names(): names = defaultdict(int) for name in sys.stdin.readlines(): name = name.strip() names[name] += 1 ...
- orf 10y agoOr just `names[name] += 1` if you're using a Counter.
- ferrari8608 10y agoWhich would of course be the optimal solution. I always forget about that object.
- tummybug 10y agoThis is where `collections.defaultdict` shines
- agentgt 10y agoI have contemplated writing a "Why You Should Learn Bash" article and then maybe "Why You Should Learn Regex" followed by "When to not use Regex". If you know bash you probably have a pretty good idea how to manage Linux and roughly how things work on Linux (you could apply this to other Bash capable OS as well). If you know Bash fairly well you probably also know how to automate many things and you probably already know regex which is fairly useful in GSD. If you know Bash you obviously know the command line well. You should learn Bash (or some shell but Bash is so ubiquitous).
- agentgt 10y agoIf only the JVM didn't have slow startup speed I would almost exclusively write Groovy scripts. There is just something so awesome about shoving your dependencies right in the script and not worrying about pip, easyinstall, system libraries etc [1]. Maybe Java 10 will fix the startup speed (or whenever jigsaw is done correctly) but I doubt it. [1]: http://docs.groovy-lang.org/latest/html/documentation/grape.html http://docs.groovy-lang.org/latest/html/documentation/grape....
- vorg 10y ago> There is just something so awesome about shoving your dependencies right in the script Groovy needs two lines (@Grab and import) to get a dependency. Golang only requires the import. Perhaps Groovy needs to be rewritten in Go.
- agentgt 10y ago> Groovy needs two lines (@Grab and import) to get a dependency. Golang only requires the import. Perhaps Groovy needs to be rewritten in Go. I'm not dissing Golang but it isn't even remotely the same. A major portion of the OP content was about using Python instead of shell scripts. Golang is not scripting language. You need to compile for each platform and the code is opaque once it is compiled. Not to mention Golang might not have dependency issues at runtime but the overarching consensus is that certainly has issues with compile time dependencies management (auto checking stuff out from github is not effective). > Perhaps Groovy needs to be rewritten in Go. I normally don't say this... but that is just dumb fanboy comment. EDIT: apparently you are working on a scripting language powered by Go called Gro. I'm not sure if you just mistyped and meant Gro instead of Go.... and reading your blog it seems you must have been severely burned by the Groovy community.
- vorg 10y ago> I'm not sure if you just mistyped I didn't mistype. Go would make a good language for writing dynamic languages such as Apache Groovy in. My own Gro is just one example of such a dynamic language.
- mxuribe 10y agoI thought this was a helpful article!
- denfromufa 10y agoI read lots of people complaining about medium-to-large scale Python projects, due to lack of static typing. Aren't people using reflection and dynamic in C#? As soon as the code touches such run-time features, the testing at run-time is essential. I wonder how large projects such as OpenStack, Django, Edx, PyPy deal without static types?