Hacker News new | past | comments | ask | show | jobs | submit login

That's not Python's fault - those are programmer errors.

Having said that, Python really has something to answer for with "encode" versus "decode" - WTF? Which is which? Which direction am I converting? I still have to look that up every single time I need to convert.

Why the heck are there not "thistothat" and "thattothis" functions in Python that are explicit about what they do?




This is something I see folks trip on. I think encode/decode are fine names actually. The problem it's that Unicode strings have decode defined at all, and similarly, that byte strings have encode defined. Byte strings should only have a decode operation and Unicode strings should only have an encode operation. Depending on your input, the wrong operations can actually succeed!


Not sure what you're talking about mate, on Python 3.6:

    >>> "hello world".decode("utf-8")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'str' object has no attribute 'decode'
    >>> b"hello world".encode("utf-8")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'bytes' object has no attribute 'encode'


I'm pretty sure I tripped up on an issue like this on python 3.7 where I double encoded or decoded something. Can't recall the exact case but it was confusing for sure.


That's good. I guess it's only in Python 2 then.


In Python 2 it works almost the same, except you only get an error when encoding/decoding doesn't work out. So I see this as an improvement.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: