BUG: np.str_ and np.bytes_ object have a confusing __repr__ and __str__
See original GitHub issue>>> s = np.str_("\0\0")
>>> list(s)
['\0', '\0']
>>> len(s)
2
>>> s
'' # wat
>>> b = np.bytes_(2)
>>> list(b)
[0, 0]
>>> len(b)
2
>>> b
b'' # wat
Caused by
It looks to me like we should just remove our overloads of these functions, they don’t make any sense.
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Why do I have to use np.string_? Why can't I ... - Stack Overflow
Simply because np.string is not defined. There's nothing special syntactically about the _, it's just a naming decision that's followed ...
Read more >NumPy 1.20.0 Release Notes
For a long time, np.int has been an alias of the builtin int . This is repeatedly a cause of confusion for newcomers,...
Read more >Bug descriptions — spotbugs 4.7.3 documentation
This document lists the standard bug patterns reported by SpotBugs. ... As most comparators have little or no state, making them serializable is...
Read more >str() vs repr() in Python - GeeksforGeeks
The print statement and str() built-in function uses __str__ to display the string representation of the object while the repr() built-in ...
Read more >Built-in Functions — Python 3.11.1 documentation
Convert an integer number to a binary string prefixed with “0b”. The result is a valid Python expression. If x is not a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
True, although that code will probably give the warning (although if you have many of that you would not notice). Its true from a subclassing perspective… It seems we either have to sacrifice consistent subclassing or consistency with arrays? Since this is NumPy scalar, which is almost an array, I think I still lean a bit towards 3. But basically there is no good solution and either 3. or 4. seem better than the current state to me.
I am for Option 3.
Any option should have the same result as
np.array(string, dtype="S")[()]
andnp.array(str, "S")
andnp.string_(str)
should be as similar as possible (at the moment at least, due toPyArray_Return
).Since it can be a bug for someone who needs the zero bytes, I think a warning makes sense. The only question is whether the warning for some reason is prohibitively noisy. I do not expect that. It could point out that
void
can sometimes be a way to spell bytes of fixed length.