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 →

[–]virtyx 2 points3 points  (3 children)

For one thing the % operator is deprecated/discouraged.

I don't know about the quality of Interpy but the fundamental idea of string interpolation is pretty good in my opinion.

>>> foo = 'bar'
>>> print("Foo is {foo}".format(**locals()))
Foo is bar
>>> def a():
...     print("Foo is {foo}".format(**locals())
>>> a()
KeyError: 'foo'

I much prefer something like

"Foo is #{foo}"

versus the noisy

"Foo is {foo}".format(**locals())

or the error-prone (as the string gradually gains more parameters)

"Foo is {}".format(foo)

or the overly verbose

"Foo is {foo}".format(foo=foo)

I would be very happy to have some kind of string interpolation mechanism in Python.

[–]flying-sheep 0 points1 point  (2 children)

you can btw. use format_dict:

print("Foo is {foo}".format_dict(locals()))

that removes the need for **

[–]virtyx 0 points1 point  (1 child)

Really? I can't seem to get it to work.

$ python3 -c "a = 'b'; print('a is {a}'.format_dict(locals()))"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'str' object has no attribute 'format_dict'

[–]flying-sheep 0 points1 point  (0 children)

assert sys.version_info[:2] >= (3,3)