I'm a new hand at python.
I tried to set system default encoding by using
"import sys; sys.setdefaultencoding('utf-f')",
but I got error message:
>>> sys.setdefaultencoding('utf-8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'setdefaultencoding'
Then I checked dir(sys), seems there was no function named
"setdefaultencoding" in "sys" module. But in python's document, it
said I should use sys.setdefaultencoding.
So, my questions: why there is no setdefaultencoding in sys module? if
I want to change system's default encoding, what should I do?
Thanks in advance
> So, my questions: why there is no setdefaultencoding in sys module? if I
> want to change system's default encoding, what should I do?
I think the answer is:
Don't.
If you do, you will break built-ins.
http://tarekziade.wordpress.com/2008/01/08/syssetdefaultencoding-is-evil/
Googling will find many discussions about this.
--
Steven
Interesting, so it has been removed from python? then why it's still
in document... It's really misleading.
Thanks for your quick answer
oh, I take back my words, it's still there, just I need to
reload(sys).
Actually, it's still there. Lurking in the corners of sys. But site.py
knows it's evil:
% python
Python 2.6.5+ (release26-maint, Jul 6 2010, 12:58:20)
[GCC 4.4.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.setdefaultencoding
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'setdefaultencoding'
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding
<built-in function setdefaultencoding>
>>>
Just don't. If you change the default encoding you are going to break
important data structures like dicts in a subtle and hard to detect way.
If your application needs to change the default encoding, it's broken.
The function is removed for a very good reaso.
Where? That may need to be changed.
--
Terry Jan Reedy