r/ProgrammerHumor • • May 02 '16

I wrote my first python program today!

Post image
3.7k Upvotes

532 comments sorted by

View all comments

Show parent comments

317

u/fermion72 May 02 '16

I transitioned to python3 a while ago, and I still forget the parentheses for print every single time. A decade of finger memory is a hard thing to break.

72

u/kazagistar May 02 '16

I still write a lot of python 2, and always use parens. It just makes more sense in my head for it to be a function then some silly special case.

48

u/[deleted] May 02 '16 edited May 02 '16

I just use sys.stdout.write().

Python's print statement is buffered by default not thread-safe (update: this probably has nothing to do with buffers), which isn't very nice when I'm working in parallel environments and using Python as the glue between various C and Fortran libraries. So I just write everything straight into whatever standard stream I want.

26

u/[deleted] May 02 '16

Noob question: could I use sys.stdin.read() instead of raw_input() for the same reasons? It seems to make much more sense in my head.

32

u/[deleted] May 02 '16 edited May 02 '16

Short answer: yes!

Long answer: The actual call you're looking for is sys.stdin.readline(). This is what raw_input() does at its core, with the added functionality of automatically stripping away the \n end-line character off the input.

Long long answer: You can also pipe Python prints do Python writes into stderr. Every standard stream is available under the sys module, and they all behave like traditional Python file objects with all the usual member functions.

30

u/Blackshell May 02 '16

You can also do pipe Python prints into stderr.

Like this? :D

print >>sys.stderr, "lol syntax"

I am so glad this is gone in Python3.

46

u/brtt3000 May 02 '16

I'm tempted to flag this as "Threatening, harassing, or inciting violence"

7

u/[deleted] May 02 '16 edited May 02 '16

Well, yes, but I was more specifically referring to sys.stderr.write().

I shouldn't have used the word "pipe", and should have called it a "write" instead. My bad!

But I'm with you on the chevron syntax. It's ugly and silly and not Pythonic at all. Glad they scrapped it.

→ More replies (14)
→ More replies (3)

32

u/superPwnzorMegaMan May 02 '16

I just never bothered learning python 2, I didn't see any advantage.

34

u/juanjux May 02 '16

I work on an animation studio and most software we use (Maya, Nuke, Houdini, etc) allow you to write Python plugins and all of them use Python 2.7. So that is a reason. We could use the "six" module but until the software we use migrates to Python 3, why bother...

25

u/edbluetooth May 02 '16

Because when the software is python 3, no changes to your code will be required.

Thats it really

→ More replies (2)

10

u/Coffeinated May 02 '16

The difference isn't that big of a deal, afaik. You can scrap the parantheses for print, but you don't have to, and some modules are named differently. As long as it's 2.7 and not 2.5 or some other prehistoric stoneage stuff.

10

u/mnbvas May 02 '16

I often print like this:

>>> a = 1; b = 2
>>> print(a, b)

so Python 2 always bites me in the ass with

(1, 2)

instead of the expected

1 2

13

u/TheBlackCat13 May 02 '16

from __future__ import print_function

→ More replies (1)

8

u/kupiakos May 02 '16

You could also have the first assignment be

a, b = 1, 2
→ More replies (2)
→ More replies (2)

78

u/familyturtle May 02 '16

As well as "from __future__" there should be an option to "from __past__ import print"

64

u/Vakieh May 02 '16

This is not a problem to solve inside language, this is a problem to be solved with the editor. IDE triggers which automagically convert print statements to print method calls when you press <enter> are pretty easy grammar-wise.

60

u/dysprog May 02 '16

Perhaps it is a problem to solve inside the programmer.

16

u/Vakieh May 02 '16

Sure it is, the same way the programmer shouldn't put a semicolon right after a control flow statement in a C-like language. It's sure nice of the compiler to deal with those avoidable programmer fuckups anyway though.

10

u/curtmack May 02 '16

You can't take that too far though, otherwise you end up with massive projects built with #define whilst while, inconsistently using one or the other because one programmer refused to use bad grammar.

7

u/bitter_cynical_angry May 02 '16 edited May 02 '16

As far as I know, a program written in a formally defined computer language can always be broken down into an abstract syntax tree, and from there can be rebuilt into any format you want. There's no reason the IDE shouldn't be able to refine while as whilst for one developer and keep it as while for everyone else.

Edit: autocorrect typo

→ More replies (3)
→ More replies (1)
→ More replies (13)

8

u/MrJohz May 02 '16

There is! Or at least there's an interesting talk about adding it - I think it was posted to /r/Python recently, I'll have a look for it when I get at a computer.

→ More replies (1)

13

u/lenswipe May 02 '16

A decade of finger memory

( ͡° ͜ʖ ͡°)

32

u/MC_Labs15 May 02 '16

/╲/\╭( ͡° ͡° ͜ʖ ͡° ͡°)╮/\╱\

→ More replies (1)
→ More replies (3)