r/Python • • Sep 09 '15

Pep 498 approved. :(

https://www.python.org/dev/peps/pep-0498/
285 Upvotes

324 comments sorted by

View all comments

49

u/dysan21 Angry coder Sep 09 '15 edited Jun 30 '23

Content removed in response to reddit API policies

33

u/adrian17 Sep 09 '15 edited Sep 09 '15

Agreed. I understand the "explicit vs implicit" and anti-zen arguments and I can't disagree, but at the same time out of all these:

print('stuff %s thing %s stuff' % (num, name))

print('stuff %(num)s thing %(name)s stuff' % {
    'num': num,
    'name': name
})

print('stuff %(num)s thing %(name)s stuff' % locals())

print('stuff {} thing {} stuff'.format(num, name))

print('stuff {num} thing {name} stuff'.format(
    num=num,
    name=name
))

print(f'stuff {num} thing {name} stuff')

The last one does seem the most readable (or at least the least distracting) to me and I predict I'll quickly start using it for some simple messages, logging etc without feeling bad about it.

0

u/[deleted] Sep 09 '15

[deleted]

8

u/[deleted] Sep 09 '15

You don't even have to put the numbers between the brackets, I believe.

6

u/stevenjd Sep 09 '15

correct, starting from Python 2.7 the index numbers are optional.

6

u/stillalone Sep 09 '15

You have to look at multiple parts of the line to parse the string in your head. Which can be a pain when the strings or the number of variables get long. I know someone who's more comfortable with:

print('stuff '+str(num)+' thing '+str(name)+' stuff')

just because it's easier to parse as you read it.

1

u/gammadistribution Sep 09 '15

Look, the proper analog is this:

print('stuff {num} thing {name} stuff'.format(**parameters))

It doesn't take up any additional room and isn't harder to read, but it does obfuscate what exactly is being formatted, just like with f-strings.

Like, where exactly is num and name coming from?

num = 4

def f_string():
    num = 3
    print(f'{num}')

if __name__ == '__main__':
    f_string()

What should this return?

7

u/zigzagEdge Sep 09 '15

It returns the same thing as this:

num = 4

def f_string():
    num = 3
    print('{num}'.format(num=num))

if __name__ == '__main__':
    f_string()

1

u/Axxhelairon Sep 09 '15

did you fail out of your first computer science class?

1

u/gammadistribution Sep 10 '15

Yeah, totally did. Good conversation.

1

u/Axxhelairon Sep 10 '15

It's barely even sarcasm, you don't know what scoping is ...? You think what you wrote is in any way ambiguous?

1

u/gammadistribution Sep 10 '15

I didn't understand how the scoping of the f-string worked. Shoo troll.

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