Re:Diagnosing a Linux-only unit test failure
by gpm on Monday October 09, 2017 @09:42AM
(#15433671)
Err... do either python or rust allow this style, in both languages I've always used
[ Parent
| on HN ]
Re:Diagnosing a Linux-only unit test failure
by cyphar on Monday October 09, 2017 @09:47AM
(#15433722)
Python added "f strings" recently (read: in 2015)[1], which look like
f"{some_var}"
which evaluates "some_var" in the local scope. Sort of like Ruby's "#{...}" (if I'm remembering that syntax correctly). Rust supports named parameters in format strings[2] but that's not quite the same.[1]: https://www.python.org/dev/peps/pep-0498/ [2]: https://doc.rust-lang.org/std/fmt/#named-parameters
Note that you can do this in old version of Python, too, using the "format-locals" idiom:
some_var = ...
"{some_var}".format(**locals())
Although this does have some limitations regarding variables to strictly being in the local scope, this is a fine idiom if you need to be compatible with older Python versions.
Re:Diagnosing a Linux-only unit test failure
by cyphar on Monday October 09, 2017 @08:47PM
(#15438534)
That is a bit of a hack though ;). You could also do it with %-style formatting as well:
some_var = ...
"%(some_var)s" % locals()
If you want to do both locals and globals you can do: some_var = ...
"%(some_var)s" % dict(globals(), **locals())
New Python versions (>=3.5) have this syntax: "%(some_var)s" % {**globals(), **locals()}
But those versions also have f-strings so that's sort of moot.
Re:Diagnosing a Linux-only unit test failure
by masklinn on Monday October 09, 2017 @12:00PM
(#15434980)
I was mostly talking about the placeholders (hence style), but
> ("lhs=({}, {}); rhs=({}, {})", lhs.x, lhs.y, rhs.x, rhs.y)
Python 3.6 added "natively interpolated" f-strings, just replace C#'s `$` prefix by `f`, and even without f-strings you could write e.g.
"lhs=({lhs.x}, {lhs.y}); rhs=({rhs.x}, {rhx.y})".format(lhs=lhs, rhs=rhs)
Rust supports named parameters but IIRC not attribute access or method calls.
Re:Diagnosing a Linux-only unit test failure
by steveklabnik on Monday October 09, 2017 @01:44PM
(#15435869)
Yes, you can't do something like this in Rust, you'd be doing what's in the quote above; named params are it. You have to do the attribute access/method call when binding the parameter, not applying it to a parameter somehow.