Changing the default string object used by the interpreter

Ryan Paul segphault at sbcglobal.net
Sat Apr 10 13:00:47 EDT 2004



> I'm wondering if anyone can suggest a way, short of directly hacking
> the
> python interpreter, to change the default str class to a different
> one. 
>   ie. So that every time a str instance is created, it uses *my* class
> instead of the built-in python string.  Is there anything hidden away 
> that can be overloaded that might make this possible to do?

I would also like to be able to overwrite built-in classes, and alter
them at run time, but Guido clearly doesnt like the idea. If you want to
do it, you will have to modify the interpreter to allow it. If you end
up doing that, I'd love to see how. FYI, this is what Guido says about
it:

"For the curious: there are two reasons why changing built-in classes is
disallowed. First, it would be too easy to break an invariant of a
built-in type that is relied upon elsewhere, either by the standard
library, or by the run-time code. Second, when Python is embedded in
another application that creates multiple Python interpreters, the
built-in class objects (being statically allocated data structures) are
shared between all interpreters; thus, code running in one interpreter
might wreak havoc on another interpreter, which is a no-no."
(from http://python.org/2.2/descrintro.html)





More information about the Python-list mailing list