Live data from Hacker News

Python has brought computer programming to a vast new audience

economist.com

251–260 of 268 posts

Re: Python has brought computer programming to a vast new audience

#251

Earlier quoted context omitted.

Here are some examples from the Python standard library: multiprocessing/pool.py: self._number_left = length//chunksize + bool(length % chunksize) test/test_math.py: tmant = tmant // (2*h) + bool(tmant & h and tmant & 3*h-1) From NumPy and SciPy: numpy-1.11.0/tools/swig/test/testArray.py:385: sys.exit(bool(result.errors + result.failures)) scipy/scipy/signal/signaltools.py:2003: n_out = n_out // down + bool(n_out % d…

Looks like the libraries use the technique to handle those conditional off-by-one situations that other languages might resolve with a ternary expression. Thanks!

FWIW, I only searched for lines containing 'bool(' and '+'. That means I missed some of the other uses, like:

  unittest2/compatibility.py:25:
    if bool(unc_path) ^ bool(unc_start):
  scipy/signal/fir_filter_design.py:266:
    pass_nyquist = bool(cutoff.size & 1) ^ pass_zero
  pypysrc/py/_path/common.py:65:
    if bool(value) ^ bool(meth()) ^ invert:
There's also a place where the bool is turned into an int, but that extra conversion isn't actually needed - the following would work without the int() call:

  scipy/stats/morestats.py:2083:
    correction = 0.5 * int(bool(correction)) * np.sign(T - mn)

Re: Python has brought computer programming to a vast new audience

#252
post #107
post #42

Earlier quoted context omitted.

issubclass(bool, int) == True in Python 2. They're not converted to ints silently they are ints.

The point is they shouldn't be. Knuth argues against implicitly treating bool as int in Concrete Mathematics.

Agreed, however you have to take context into account. Python 2 didn't always have a bool type (granted, already a mistake), before it was introduced int was used instead. Making bool a subclass of int made it possible to use it even in combination with earlier code, which made adopting it trivial.

If bool wouldn't be a subclass of int, some projects probably would still be using int instead of bool.

Re: Python has brought computer programming to a vast new audience

#253
post #230

Earlier quoted context omitted.

Thank you for this. I've been typing out: :set tabstop=4 :set shiftwidth=4 :set expandtab Like a pleb for years. What's sts?

Put them in your .vimrc, and put your dotfiles in git. Hint: searching GitHub for "dotfiles" can be a fun and productive way to spend a Friday afternoon :-)

I mean locally yes, but I'm poking around on servers frequently enough and writing python there (Hey, I'm no saint) that it gets committed to memory eventually.

Re: Python has brought computer programming to a vast new audience

#254
post #201

Earlier quoted context omitted.

If it wasn't possible you could still do: sum(x > 3 for int(x) in iterable) And avoid a class of errors in other places.

That code is invalid, because you're using "int(x)" as an assignment target. What would you expect it to do?

sorry:

    sum(int(x) > 3 for x in iterable)

Re: Python has brought computer programming to a vast new audience

#256
post #57

Earlier quoted context omitted.

It is not a problem, it was just non expected behavior for someone trained in strong typed languages. For example, you want to calculate the performance of your ML algorithm by counting the correct predictions / total. My expectation was that the result of a division of integers, would be a float, specially if the modulo is not 0.

In what strongly typed language does the division of two integers result in a float?

The language of the real world?

Nobody doing maths with pencil or paper will think 3/2 equals 1.

More to the point, the symbol 1 represents a float as much as it does an integer.

Re: Python has brought computer programming to a vast new audience

#257

Earlier quoted context omitted.

I am involved in many, many language communities. I've shipped software to production audiences of varying sizes in C, C++, Perl5, Common Lisp (Allegro), Common Lisp (SBCL), Ruby, Python, Scala, Ocaml, Java, JavaScript, Clojure, Haskell, F#, Typescript, Purescript, and Pony (finally!). I've used many more languages for non-trivial projects, including some now some defunct but really beautiful contenders like Nim, Io,…

Uh? Is Nim defunct? I wouldn't say so!

Yeah I miswrote it. I meant to phrase it differently. I love most languages on that list and didn't see my on-mobile editing had caused me to say Idris is defunct until it was too late. :(

Re: Python has brought computer programming to a vast new audience

#258

Someone else said this on HN: Python is the second best language in almost every field of computing. That's why there's a library for everything in Python. It's limitations are also not terrible for most people. If you want super fast, you will know how to do that some other way. If you just want simple and easy to read, you use Python.

This is likely a retread of the old saying that the WordStar commands were everyone's second favorite key bindings - http://www.mischel.com/diary/2005/08/22.htm .

For that matter, I remember an episode of Magnum PI where the person who hired Magnum had talked to a few other PIs, and hired Magnum because he was always second on their lists of good PIs.

Re: Python has brought computer programming to a vast new audience

#259
post #225

Earlier quoted context omitted.

> There are 2 kinds: bytes and unicode. No I mean single quote, double quote, triple double quotes, r prefix, u prefix, f prefix, combinations, etc > Can you give some examples The one I have in mind is the os library, which I use a lot (like why st_mtime?).

>No I mean single quote, double quote, triple double quotes, r prefix, u prefix, f prefix, combinations, etc These are all extremely helpful, though. All of those quote combinations are equivalent, and can be used to prevent having to escape quotes within the string. The u prefix is gone from Python 3, r is a raw string (which many other languages have, like C#'s @ prefix), and f is a new feature which generated a lo…

You wrote "These are all extremely helpful".

I disagree, in that I think they are only somewhat helpful. If we only had double-quoted strings, and not single-quoted strings, then I think there would be little difference to the language or its quality of use.

Going back to the question of "why st_mtime?" - that's because os.stat() is a wrapper around the POSIX stat(2) system call. My FreeBSD man page defines it as:

   st_mtim   Time when file data last modified.  Changed by the mkdir(2),
             mkfifo(2), mknod(2), utimes(2), write(2) and writev(2) sys-
             tem calls.
The Python implementation of stat for MS Windows is basically doing its own emulation layer for POSIX compatibility.

Re: Python has brought computer programming to a vast new audience

#260
post #197

Earlier quoted context omitted.

:set ts=8 sts=0 sw=8 expandtab I have secrets.

Go one step further and add these to you vimrc: command Tab1 set ts=1 sts=1 sw=1 command Tab2 set ts=2 sts=2 sw=2 command Tab4 set ts=4 sts=4 sw=4 command Tab8 set ts=8 sts=8 sw=8

slight addition:

    command! Tab1 set ts=1 sts=1 sw=1
    command! Tab2 set ts=2 sts=2 sw=2
    command! Tab4 set ts=4 sts=4 sw=4
    command! Tab8 set ts=8 sts=8 sw=8
The ! exclamation mark is there to prevent vim from complaining about a duplicate command already existing whenever you reload your vimrc.
Post reply on HN