They are, so you come up with a strategy to deal with them. Set your encoding at the boundary of your code and stop touching it.
Better error messages would be great, but I'd rather have horrific error messages as opposed to autoconverting madness. It's not possible to fix Python 2 code using just Python 2 if you only throw a unicode character through a function 1% of the time.
I like how easy Python 2 is, but strings are broken. It took me forever to figure out my data files are not in utf-8, but rather latin-1. They are not compatible.
Who really uses bytes anyways? Not beginners. They just set an encoding and they're done. You can just pretend your data is always in say latin-1 or utf-8 and it's probably going to work on an ASCII string. Bytes are a higher level feature. People just got used to using 'wb' because they didn't want stupid \r characters at the end of the line.
Too Many Formatting Options
Why would I ever use .format(...)? It's worse than %s and slower. There are now 2 methods in my book and 1 that I can use because I write Python 2/3 code.
Why would I ever use .format(...)? It's worse than %s and slower. There are now 2 methods in my book and 1 that I can use because I write Python 2/3 code.
Python is already a language that sacrifices performance for legibility. The case of %s vs. .format() is, as you put it, a case of performance versus legibility. The latter is easier to read and therefore more pythonic.
You can use either with Python 2 and Python 3; .format() was introduced with Python 2.6. The new incompatibility are f-strings.
There should not be three ways to format strings, you're correct. It's not pythonic. But .format() isn't the one that should go.
All of that aside, if you're concerned about performance, use PyPy.
.format() also has more formatting capabilities, supports more types, and can be leveraged to support more types using the format method, I believe. Also, .format() was roughly 2.5x slower than %s in my benchmarks, but it seems a fair trade-off for the new syntax and capabilities.
But actually, I find %s to be easier to read as they're more akin to other scripting languages string interpolation syntax, but f-strings are king in my opinion. Can't wait for 3.6!
%s forces you to repeat after yourself. The plugged-in value knows its type, so why do you have to tell it "you are a string"? It's code smell, plain and simple.
Not to mention %s is not really like interpolation, unless you use the %(name)s syntax, but format can do this too with {name}. And I'd argue that bash's ${xyz} is much more similar to {xyz} than it is to %(xyz)s
The latter (.format()) is easier to read and therefore more pythonic.
See I disagree with that. I have to make a dictionary that I don't have rather than doing something like 'x={x} y={y}' % (y, x)` where the code is smart enough to see that I wrote the variables backwards.
You can use either with Python 2 and Python 3; .format() was introduced with Python 2.6. The new incompatibility are f-strings.
I know. I think f-strings are great, but I can't use them because I support Python 2.7.7+. It's .format() that I find hideously verbose.
There should not be three ways to format strings, you're correct. It's not pythonic.
I don't actually mind that. If it's useful, keep it. There's also now going to be 4 methods if you include str.Template(), which I just learned about today. It's older than .format(). I just want something that's terse and clear.
All of that aside, if you're concerned about performance, use PyPy.
Unfortunately numpypy, scipypy, matplotlibpypy, PyQt5pypy, and VTKpypy are not a thing. PyPy uses a very restricted set of Python. Shoot, it doesn't even support past Python 3.3. Python 3.3 is about to be lose support in numpy; it's old.
I have to make a dictionary that I don't have rather than doing something like 'x={x} y={y}' % (y, x)` where the code is smart enough to see that I wrote the variables backwards.
One library I maintain has some functions which -- because they're implementing algorithms from a web standard, and the standard is only defined in terms of Unicode -- must enforce that their arguments are str and not bytes on Python 3. And I realized that %-formatting is implemented on bytes (as of 3.5) but the format() method isn't (and never will be). So I simply changed the string-formatting operations over to format() and voila! Now any attempt to pass in bytes will raise an exception.
Why would I ever use .format(...)? It's worse than %s and slower. There are now 2 methods in my book and 1 that I can use because I write Python 2/3 code.
That's interesting. I never use % formatting. It's a fairly gross overload of the modulus operator, and has weird syntax. str.format is explicit and clear, and if it's slower than % it's negligible.
10
u/billsil Nov 24 '16
They are, so you come up with a strategy to deal with them. Set your encoding at the boundary of your code and stop touching it.
Better error messages would be great, but I'd rather have horrific error messages as opposed to autoconverting madness. It's not possible to fix Python 2 code using just Python 2 if you only throw a unicode character through a function 1% of the time.
I like how easy Python 2 is, but strings are broken. It took me forever to figure out my data files are not in utf-8, but rather latin-1. They are not compatible.
Who really uses bytes anyways? Not beginners. They just set an encoding and they're done. You can just pretend your data is always in say latin-1 or utf-8 and it's probably going to work on an ASCII string. Bytes are a higher level feature. People just got used to using
'wb'because they didn't want stupid\rcharacters at the end of the line.Why would I ever use
.format(...)? It's worse than%sand slower. There are now 2 methods in my book and 1 that I can use because I write Python 2/3 code.