str and unicode proper usage

Lie Ryan lie.1296 at gmail.com
Mon Dec 14 14:47:02 EST 2009


On 12/15/2009 5:05 AM, gizli wrote:
> Hi all,
>
> If an entire application operates on Unicode strings from UI to
> database, is there a use case for str() and unicode() functions? The
> application should be able to read/write files, open sockets and
> execute external processes and parse their output. From my own
> experiments, the open() command for files accepts unicode strings. I
> am just wondering if there is a place where str() would have to be
> used, other than the usual use case of converting a non-string python
> construct (such as an integer) into a string.
>
> The reason I am asking is, I work on a project with several other
> developers and our NLS testing is not going so well. Major reason is
> (I think) that there is a lot of str() functions interspersed
> everywhere. So whenever a unicode character is used in those
> variables, the application breaks. My recommendation to the team was
> to remove these functions and only leave the necessary ones. However,
> I do not have a generic answer on when a str() function is necessary.
>
> Thanks!

str() is rightly renamed byte() in python 3. The new name reflects its 
real role: as generic binary string. Use str()/byte() to represent 
non-textual stream of binary data (e.g. audio/video stream, .doc file, 
or cipher-text).



More information about the Python-list mailing list