default setting of unicode set

Martin v. Löwis loewis at informatik.hu-berlin.de
Mon Jul 29 05:21:29 EDT 2002


mathias palm <mathias.palm at gmx.net> writes:

> It is possible to set a default unicode (set to ascii) in
> site.py. This is clearly a setting by site. For programs which my use
> different unicode charackter set it would be useful to have a per user
> or better still a per program setting. I did not find any possiblity
> to do this. The function,  sys.setdefaultencoding() which sets the
> default unicode is even deleted in site.py.

That is on purpose: Explicit is better than implicit. If you have a
Unicode string, and you want to convert it to a byte string, invoke
its .encode method, instead of implicitly relying on automatic
conversion.

> I am sure there is a reason for this behaviour, I cant see it
> however. Can anybody explain or tell me where to read about it? Why
> not setting the default unicode in a file like .pythonrc?

The user's preferred encoding is already available to a Python
program; there is no need to require the user to put her preferred
encoding in yet another place.

Unfortunately, it is not straight-forward to obtain the preferred
encoding in a portable way. Here are some strategies:

- on Unix, use locale.nl_langinfo(locale.CODESET) to find the user's
  encoding. Notice that you may have to invoke setlocale for this to
  work correctly (e.g. on Solaris), and that not all Unix variants
  provide this function.

- on Windows, use locale.getdefaultlocale, ignoring the language
  result of that. This actually may work on Unix, too - but then, it
  may also fail.

> Is there a possibility to set a unicode default later on? 

No. Just don't use the default. Be explicit about your encodings in
your program. Use the user's preference where appropriate - only the
application programmer can know whether using the user's preference
*is* appropriate (for example, some data formats may require a fixed
encoding, such as UTF-8, independent of the user's preference).

Regards,
Martin



More information about the Python-list mailing list