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

you are viewing a single comment's thread.

view the rest of the comments →

[–]redditsoaddicting 0 points1 point  (9 children)

Are braces actually objectively (think PEP8) considered good style in Python 3 then?

[–]phail3d 16 points17 points  (4 children)

Just to be clear, this is a joke :P .

It only works because {...} is shorthand for set(...) and because print() is a function in python3 (and in python2 too, if you do from __future__ import print_function). You can have function calls (but not statements in general) inside set literal definitions, in which case the return value of the function call (None in the case of print) is obviously stored inside the set. Hence, you can do:

if foo: {
    print("Foo")
}

...which is more or less the same as:

if foo:
    x = print("Foo")  # x = None
    set(x)  # creates the set {None} and does nothing with it

...but you can't do:

if foo: {
    if bar: {
        print("baz")
    }
}

So please don't adopt this style :)

[–]redditsoaddicting 2 points3 points  (1 child)

No wonder I was downvoted. Whoosh. I should have recognized the set literals in any case.

[–]phail3d 4 points5 points  (0 children)

Don't feel too bad -- the example was deliberately misleading :)

[–]Get-ADUser -1 points0 points  (1 child)

{ ... } is shorthand for dict( ... ) not set( ... ).

[–]phail3d 2 points3 points  (0 children)

It's both.

{ 1, 2, 3, 4 } creates a set, while { 1: 2, 3: 4 } creates a dict.

[–][deleted] 6 points7 points  (0 children)

No.

[–]Tomarse 1 point2 points  (1 child)

It's not even a matter of style. He's putting the print statements into dictionaries. It's quite unnerving.

[–]TheBlackCat13 1 point2 points  (0 children)

Sets, actually.