>> from java.lang import String
>> def the_java_way(binary_data, encoding='utf-8'):
>> if encoding:
>> return String(binary_data, encoding)
>> return String(binary_data)
> This looks bad to me -- because encoding has default value, "else" branch
> of "if" will IMHO never be executed
No, it works exactly as expected; call it as follows
data_string = the_java_way(binary_data, None)
> and the string without encoding will
> be automagically converted to UTF-8 (which is a good thing IMHO, but
> probably not what was intended).
Yes, the default case is to treat the bytes as being encoded in utf-8.
Making it the default assumption that bytes have some encoding is the
right thing to do, because all bytes representing strings have some
encoding. Using the above function definition forces the user to
specifically state that there is NO encoding, which is rarely true;
thus the programmer is forced to do some serious thinking about where
the data comes from and what encoding it is in, which is exactly what
I intended for the function.
Making the default encoding UTF-8 is a sensible default, and also
exactly what I intended to do.