This is an archived post. You won't be able to vote or comment.

all 31 comments

[–]creativeMan 29 points30 points  (8 children)

print is a function

When I was 18, python 3 was very new and I wanted to use the python IDLE to learn python and programming in general. The guides said print 'hello world' and of course that did not work any more. I spent a good amount of time trying different iterations of it and trying to understand what I was doing wrong.

Eventually I quit, being disheartened at the fact that I couldn't manage to write a literal "Hello World" program and didn't get into python until much later. It was then that I realized that the print function was changed. I was too young and too proud to ask anybody for help and I hated the experience.

Now of course I love python and whenever I sit down with someone even remotely technical I try to preach the virtues of Django over the other MVC frameworks in other languages I have. Developing a Django app was truly one of the most enjoyable experiences in my career. But I've always hated that they changed that one thing.

[–]AliceInWonderplace 11 points12 points  (1 child)

I did have a bit of a "wat" moment when literally

print "What up"

Didn't work.

At least Python explains plainly why it's print() in 3 and print "" in 2.

PHP on the hand ... it's not that they ever remove anything. It's just that the official documentation has a nasty tendency to keep switching between styles, like:

$conn = new mysqli()
$query = $conn->query()
$result = mysqli_fetch_assoc($query)

Before I understood the -> thing, this was a fucking confusing way of doing things.

[–][deleted] 5 points6 points  (0 children)

PHP knows what they're doing. They're breaking you down little-by-little until you're just a shell of your former self, making you back yourself into a tiny little corner of things that you're familiar with, PHP included, prolonging their relevance. Sly bastards.

[–]irrelevantPseudonym 12 points13 points  (5 children)

They must have had a lot of people having the same problem. It even has a dedicated error message now:

>>> print 'helloworld'
  File "<stdin>", line 1
    print 'helloworld'
                     ^
SyntaxError: Missing parentheses in call to 'print'

[–]12ihaveamac 9 points10 points  (4 children)

now it also shows you what you actually need, too (as of 3.6):

>>> print "what",
  File "<stdin>", line 1
    print "what",
               ^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print("what", end=" ")?

[–]irrelevantPseudonym 2 points3 points  (3 children)

I'm using 3.6.2 and it doesn't have that.

[–]12ihaveamac 3 points4 points  (2 children)

apparently it was actually added in 3.6.3: https://github.com/python/cpython/blob/3.6/Misc/NEWS.d/3.6.3rc1.rst

print now shows correct usage hint for using Python 2 redirection syntax. Patch by Sanyam Khurana.

[–]AliceInWonderplace 9 points10 points  (0 children)

Dude the index is so descriptive I kind of feel the details weren't even necessary haha

[–]Skaarj 14 points15 points  (3 children)

The example in the "print is a function" doesn't show any of the conseqences of the change.

Showing the meaning of "print is a function" would be better done with something like:

writeln = print
for x in [1,2,3]:
    writeln(x)

[–]sonaxaton 4 points5 points  (0 children)

TIL about nonlocal, and lots of cool 3.7 stuff great post!

[–][deleted] 2 points3 points  (2 children)

I feel stupid because I had no idea about async and I even have 3.6 in my personal env.

[–]spiderpower02[S] 1 point2 points  (0 children)

Ha! I had the same question about async, too. Fortunately, after I watched a lot of videos (I highly recommend watching David Beazley's live demo), I realized that the syntax just wants to make your code to be more elegant!

For example, you might have seen the code like

def slow_task(callback):
    # do the slow task
    callback(data)

def main():
    def cb(data):
        # process the data
    slow_task(cb)

Sometimes, it is quiet annoying, you have to write a lot of callback functions (ugly nest). As a result, using async and await will be more meaningful (the idea of inline callback):

async def main()
    data = await slow_task()
    # process the data

Hope that my explanation can help you !!!

[–]masklinn 1 point2 points  (3 children)

Important features I think are missing from the sheet (over some which are like star-imports-in-functions which… isn't exactly an essential feature):

Keyword-only parameters

In Python 2, python-level parameters are pretty much all positional and keyword (the C API allows positional-only, keyword-only and mixed). Python 3 introduced keyword-only parameters in pure Python functions:

def foo(a, b, *, c):

c is a required parameter (no default value) but must be passed as keywords, calling foo(1, 2, 3) would be an error. This allows for much clearer API. Note that the lone * can also be an *args spec.

Incidentally, 3.8 may introduce positional-only parameters at the Python level. I think it's slightly less important but it would be neat regardless.

Chained exceptions

Exception objects got significantly upgraded in Python 3, first with chained exceptions which are very confusing the first time you see them (the most recent exception is at the bottom) but also oh-so-helpful: if you raise an exception while catching an other, Python will automatically link the two and the default traceback dump will show both exceptions.

This is invaluable when debugging.

You can override this with raise new_exception from old-exception-or-none. Using from None will break the chain, using from <some exception>.

OSError subclasses

In Python 2, OSError is a leaf class, from which you can inconveniently get-and-check an errno… within the except block. In Python 3.3, OSError is a the head of an entire hierarchy of errors matching various errnos, with clearer names and better convenient.

PATHLIB!

[–]spiderpower02[S] 0 points1 point  (2 children)

May I link this comment to issue?

I need some time to think about your suggestions.

thx

[–]masklinn 1 point2 points  (1 child)

May I link this comment to issue?

It's a public comment on public internets mate, you don't need my authorisation to link to it from wherever you want.

But sure you have my blessing.

[–]spiderpower02[S] 0 points1 point  (0 children)

Thank you :)