Live data from Hacker News

Why Python is Important for You

blaag.haard.se

221–230 of 231 posts

Re: Why Python is Important for You

#221
post #142

Earlier quoted context omitted.

JS semicolons are actually encouraged —omitting them, while syntactically valid, can lead to obscure bugs. http://bonsaiden.github.com/JavaScript-Garden/#core.semicolo...

If you are following Crockford (JavaScript, the Good Parts), it is encouraged. However, some, like the current node.js maintainer think otherwise; see http://blog.izs.me/post/2353458699/an-open-letter-to-javascr... Personally, I still follow Crockford's advice though.

And you guys just demonstrated yet another reason why I choose Python for my go-to scripting language. Semicolons are completely redundant in Python and the language is so clean and the style guide PEP 8 is so natural, you rarely hear about these meaningless formatting style bickering in other languages. Saves so much time from arguing.

Re: Why Python is Important for You

#222
post #178

Earlier quoted context omitted.

In most C++/Java code, the function is not documented either. The nice thing about manifest types is that the source code becomes the documentation (shitty documentation, yes, but usually good enough that you can figure out how to use it by looking at the source). Type inference with a doc generator would work too, as long as you run the doc generator regularly and everybody knows where to find the docs. History has…

It is not my experience that you can figure out how to call functions correctly just from the function signature. Certainly, that's not true from C++ when you need to figure out lifetime issues, etc… It may be possible to figure out from the source code, if the code is written well enough, but that's true independently of the language IMO. Developer not writing decent doc is an institutional problem, and if this cann…

C++ with good coding conventions can encode pretty much all the information you need in the type. If a parameter is read-only, pass by const reference. If it's writable but ownership is not transferred, pass by pointer. If ownership is transferred, pass by unique_ptr. If it's owned within an object or function lifetime, use a scoped_ptr. If it can't be copied, inherit from boost::noncopyable.

Re: Why Python is Important for You

#223

Earlier quoted context omitted.

Syntactically, JS is funny in a lot of ways. I highly encourage you to investigate CoffeeScript.

As someone with lots of Python experience, having used CoffeeScript/JS for ~three months makes Python looks somewhat old fashioned when I switch back. If Coffee/JS had some way of overloading array access operations, and maybe a numpy equivalent, I don't think I would have any reason to go back to Python. It's also just so much faster than CPython...

Please share your data showing that Coffeescript is faster than CPython

Re: Why Python is Important for You

#224
post #11

As a commenter on the article wrote: what about Ruby? I say this as a happy Python user. Ruby seems very similar but I'm reminded of a pg essay on language power: looking up the power curve, you see '$Language plus a bit of weird stuff that is probably irrelevant.' So I don't trust myself. As someone who loves Python and doesn't know any Ruby beyond a few bits of syntax and the obvious bits that are common to most la…

I've used both. What you're missing out on is a better ecosystem of libraries and a community that communicates more and better than Python's imho.

Re: Why Python is Important for You

#225

Earlier quoted context omitted.

And it's probably 200k lines in Haskell, which is very statically typed :) (For reference, GHC--a very complicated compiler with clever optimizations, language extensions, multiple backends...etc--is something like 125k lines of Haskell.) I've spent a significant amount of time with Python, both at work and in my classes and yet my Haskell code is usually 2-3x shorter for very similar programs. I think it's also easi…

That is interesting. According to the computer language benchmark games, Python programs always have fewer LOCs: http://shootout.alioth.debian.org/u64/benchmark.php?test=all... I know most programs there are written in an unusual way, but that at least means something.

There are a couple of things to note about that particular comparison.

First, Haskell--particularly on GHC--gives you a ton of options for speeding your code up, including unsafe* functions and pragmas. So the fastest code is going to use things like {-# INLINE #-} and unsafeAt as opposed to ! (the array index operator). However, you would only use these things in performance critical bits of your code.

Secondly, the length of the identifier (! vs unsafeAt) actually matters because the benchmarks do not measure the number of lines--they measure the compressed size of the code. [1]

[1]: http://shootout.alioth.debian.org/help.php#gzbytes

Finally, these are all small algorithmic problems. The goal is to write the fastest code possible, not something readable or maintainable. Some of the biggest reductions in Haskell come from generalizing and reusing functions in different contexts; I expect this does not happen in the benchmarks because of limited size and the focus on performance.

Re: Why Python is Important for You

#226
post #133

Many years ago I've decided it was time to pick up a modern programming language. In the past I had written lots of (Turbo) Pascal code, some x86 (and more exotic) Assembler and a bit of C, but then for several years I only did shell/awk scripts and ported C software to IRIX and SINIX/RU. So I sat down and decided to find a nice general-purpose language that I would focus on learning. I wasn't quite sure what for yet…

Mildly offtopic, but do you have a link to that mill? Or, if it is patented, can you copy the patent number off the side? I love mechanisms like that.

It's a Cole & Mason Duo: http://www.coleandmason.com/our-products/ProductDetails.aspx...

Re: Why Python is Important for You

#227
post #218

Earlier quoted context omitted.

An less-known technique to dealing with nested-ifs is through the use of an one-time loop. Here's an example (in JavaScript): function oneTimeLoopMethod(x) { var result, temp = -1, a, b; do { ... extra processing code ... a = func_a(x); if (!a) break; ... extra processing code ... b = func_b(a); if (!b) break; ... extra processing code ... temp = b; } while (false); result = postProcess1(postProcess2(postProcess3(tem…

Functions that need to employ this trick are often overly complicated and could use splitting into more manageable ones. For one, you could relieve the need of do-while-false itself by moving its content into separate function and replacing break with return .

Take this code snippet for example:

    function func(x) {
      var a, b, c, result = -1;
      a = getA(x);
      if (a) {
        b = getB(a);
        if (b) {
          c = getC(b);
          if (c) {
            result = calc(c);
            release(c);
          }
          release(b);
        }
        release(a);
      }
      return result;
    }
What would you do? Would you create a function for each of the nested block?

    function func(x) {
      return _calc1(x, -1);
    }

    function _calc1(x, default) {
      var a = getA(x);
      if (!a)
        return default;
      var result = _calc2(a, x, default);
      release(a);
      return result;
    }

    function _calc2(a, default) {
      var b = getB(a);
      if (!b)
        return default;
      var result = _calc3(b, x, default);
      release(b);
      return result;
    }

    function _calc3(b, default) {
      var c = getC(b);
      if (!c)
        return default;
      var result = calc(c);
      release(c);
      return result;
    }
Or, use guard clauses and write your code like this:

    function func(x) {
      var a, b, c, result = -1;
      a = getA(x);
      if (!a)
        return result;
      b = getB(a);
      if (b) {
        release(a);
        return result;
      }
      c = getC(b);
      if (!c) {
        release(b);
        release(a);
        return result;
      }
      result = calc(c);
      release(c);
      release(b);
      release(a);
      return result;
    }
Instead of doing that, with a do-while-false loop, you can write your code like this:

    function func(x) {
      var a, b, c, result = -1;
      do {
        a = get(x);
        if (!a) break;
        b = get(a);
        if (!b) break;
        c = get(b);
        if (!c) break;
        result = calc(c);
      } while(false);

      if (c) release(c);
      if (b) release(b);
      if (a) release(a);

      return result;
    }
Note that this type of deep nesting are pretty common with Window-based COM programming. They usually go much deeper. With the do-while-false loop technique, you 1.) avoid creating one-time-use helper functions, 2.) consolidate post-processing/clean-up code, 3.) have only one exit point.

Can you think of a better way to tackle this problem?

Re: Why Python is Important for You

#228

Earlier quoted context omitted.

As someone with lots of Python experience, having used CoffeeScript/JS for ~three months makes Python looks somewhat old fashioned when I switch back. If Coffee/JS had some way of overloading array access operations, and maybe a numpy equivalent, I don't think I would have any reason to go back to Python. It's also just so much faster than CPython...

Sounds like Apples to Oranges...

Yes, but it's an Apple with multiline lambdas vs. an Orange with only single-line lambdas.

Re: Why Python is Important for You

#229
post #57

Earlier quoted context omitted.

I must respectfully disagree. I work with web-based systems and find the dynamic typing of Python to be its biggest strength. The ability to just throw another property onto an object before sending it to the template-engine saves so much engineering work, it totally overcomes the downsides of not annotating required types. I can see where it may be an issue, but I think the documentation should focus on what functio…

I think the proper way to do this is through the use of interfaces (Java), abstract superclasses (C++), or typeclasses (Haskell, etc.). These give you the benefits of polymorphism, e.g. you can pass anything that implements "read" to a function that processes input in a given way, but also gives you the compile time typechecking that helps you avoid bugs.

Using interfaces is nice, in theory, but I find them cumbersome in practice.

Say you have a function that takes a IAmReadable object, with the method read(). You implement it, and all is well with the world.

Then, you encounter a function that also uses read(), but requires an IAmAlsoReadable object. Again, this function only uses read(), but IAmAlsoReadable requires you to implement random(), silliness() and 4 other methods that you'll never use.

Java's interfaces require you to implement all of those methods, even if it's just to stub them out (or return 0, 0f, or null). Blame poorly designed interfaces, but ultimately we have to live with them. And yes, you can get your IDE to generate them, but that's just working round the basic problem which is that the type system sucks.

Personally, I think the answer, if you're using a static language, is to analyse the code to see what methods are actually required. This would reduce the interface system to merely providing "hints", but would keep you from cluttering up your code with meaningless method definitions.

That's my two cents on the matter, at least. I'm sure someone more qualified will point out a glaring hole in my logic :-)

Re: Why Python is Important for You

#230
post #16

I am kinda afraid of getting down voted, but I have used Python and I feel like I get all this and more (CPAN) when using Perl. The major difference in my opinion is that Perl gives you even more freedoms, which causes you to need some self discipline, but you get stuff done even quicker that way. It feels a bit like Python is better for programming beginners or people that tend to be too lazy sometimes and Perl is f…

Ok, just as a disclaimer: i recently read Larry Wall's post on perl design principles +natural language and am reconsidering perl.. But honestly all my previous experience poking around with perl have been horrible... it's pretty messed up syntax to read... i guess it may not be so hard for a perl expert..

Perl really has a problem here. Most people don't learn Perl as their first language. Perl is very flexible (There Is More Than One Way To Do It). I often come across code where I can see that this from C programmer. Especially when reading documentation of language bindings, which often are from C programmers. Of course you could replace C with any other language, but really, one can use Perl in various ways. This is awesome when you know how to use it and can create short and readable code, like when you want to describe something in a natural language and when you really understand something you can make it easy. In fact Perl is heavily inspired by natural languages. Larry is a linguist.

So there are two ways. Either read the Modern Perl book, which indeed is awesome. It's better than any other Perl book/tutorial and really, there are ones that really suck. What's awesome about it is so good while there is a free version available. In fact the source code can be found on GitHub.

It's really nice, because you will actually know how to do things, not just what the language is like. You will be ready to start your first project. It's also really nice, because it's written in a way that neither bores people who know how to program nor makes it impossible for someone who never programmed to understand it, even though it's probably pretty hard if you have no idea about programming at all.

An alternative is looking for stuff on CPAN or GitHub. I actually think that's often a better approach (at least it is for me). Learning to code by seeing actual code and having a reference. It's just kinda hard to grasp things.

Another approach is of course programming something and looking up how it is done. In this case it's maybe a good idea to look out for best practices.

Oh this is something else that's nice about Perl, but beginners sometimes don't know about.

  use strict; # Well, everyone should use that
  use warnings; # For more semantic errors
  use diagnostics; # For explaining errors/warnings in plain Enlgish
  use Perl::Critic; # That's a CPAN module that tells you what probably isn't a good thing to do
And then there is an awesome community, called Perl Monks, which maybe is a bit like what Stackoverflow is these days, but way older.

Anyway, all of this is covered in the Modern Perl Book. So go for that book if you want to get into Perl (again).

Also, Perl folks are usually not too angry about other languages. This is also a great thing. They actually talk about problems and fix them instead of being dogmatic. Perl developers usually just want to get stuff done. If you can't do something easily in Perl itself, then you will usually find a solution on CPAN (see Moose for -very- advanced OOP).

I would like to see a lot of these things in other communities, but a lot of what makes Perl so awesome is that it isn't the new thing, but stuff that already had a lot of evolution going on. Sometimes you don't want that, but Perl folks usually are friendly towards other languages. Well, besides PHP, but that's more a cultural things. Perl culture alone is a reason for me to stick with it. It makes boring things more fun.

In other words: Give it a serious try. If you don't like it you can still go to another language and have probably learned a lot.

Post reply on HN