Changing the default string object used by the interpreter

M

Mike McGavin

Hi everyone.

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?


For some context, I've been hacking away on an experimental pet project
for a while. I have a class that I call zstr, which inherits the
standard python str class, adding some state information. The idea is
that strings can have several representations, such as different ways
that they're currently escaped, even though all different
representations are essentially the same string. If the string contains
information about its current state, it might be possible to make
defensive coding in things like web scripting a bit easier. eg. Any
code that's unsure of the string's state can re-escape it to make sure,
whilst the string instance itself can ensure that it doesn't
accidentally get escaped several times in different parts of the code.
My somewhat inefficient, buggy and badly documented prototype module is
available at [ http://www.mcs.vuw.ac.nz/~jester/zstr/ ].

One problem I've discovered anecdotally, however, is that many of the
strings I'd *like* to naturally treat as zstr objects are ones that are
initially generated as ordinary strings from external code, such as the
built-in libraries. Explicitly wrapping every single string that my
code receives into a zstr constructor is both ugly and somewhat
unreliable, since they're so common that I often forget. Therefore I'd
like to experiment with ways to bypass this.

What I'd like to try, as mentioned above, is to override Python's string
creation code, so that it automatically uses a zstr class instead of the
str class for every string created in the system. I guess the one
exception would be where my zstr class inherits and (in some cases) uses
the regular python str class internally, in which case the real str
would need to be able to be referenced.

If anyone can suggest a way to do it, I'd love to hear it.


Thanks for any help.
Mike.
 
J

Josiah Carlson

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?

Mike,

I don't believe so. There would need to be quite a few changes to the C
internals of Python in order to make /every/ string created or generated
be of your string class.

I would suggest you just wrap the Python strings with your own class
whenever you need it...

def myfunct(arg, ...):
if isinstance(arg, str):
arg = MyClass(arg)
#rest of the function body.


With the upcoming decorator syntax for 2.4, you could decorate all of
your functions to do this, or if you are careful, you could use the
below now (as long as MyClass was a subclass of object)...

def auto_wrap(*arg_types):
def funct(function):
def funct_with_arguments(*args):
a = []
for typ, arg in zip(arg_types, args):
if isinstance(typ, type):
a.append(typ(arg))
else:
a.append(arg)
return function(*a)
return funct_with_arguments
return funct

#using current syntax..
def myfunct(arg):
#rest of function body

myfunct = auto_wrap(MyClass)(myfunct)

#using Guido and other's preferred decorator syntax...
[auto_wrap(MyClass)]
def myfunct(arg):
#rest of function body

#using another group's preferred decorator syntax...
def myfunct(arg) [auto_wrap(MyClass)]:
#rest of function body


- Josiah
 
P

Peter Otten

Mike said:
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?

You could have a look at Quixote, which changes (literal) strings to
htmltext in their .ptl templates. It's done by manipulation of the AST
(file ptl_compile.py). That is the least intrusive strategy I can think of.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top