Subclassing built-in classes

Steve Holden steve at holdenweb.com
Thu Oct 5 14:24:04 EDT 2006


Maric Michaud wrote:
> Le jeudi 05 octobre 2006 15:52, Steve Holden a écrit :
> 
>>>But what prevents to interpret literals as a call to __builtins__ objects
>>>and functions ? optimization ? what else ?
>>
>>
>>When are literals interpreted? During translation into bytecode.
> 
> 
> agreed, but what's the problem with this ?
> 
> We can actually monkey patch all buitins like in this example :
> 
> In [1]: oldstr=str
> 
> In [2]: class mystr(str) :
>    ...:     def __new__(*a, **kw) :
>    ...:         print 'called : ', a, kw
>    ...:         return oldstr.__new__(*a, **kw)
>    ...:
>    ...:
> 
> In [3]: import __builtin__
> 
> In [4]: __builtin__.str = mystr
> called :  (<class '__main__.mystr'>, <ItplNS 'In 
> [${self.cache.prompt_count}]: ' >) {}
> called :  (<class '__main__.mystr'>, 5) {}
> ....
> 
> 
> If the generated bytecode of {k:v} is more or less the same as the one 
> gernerated by dict(((k,v))), monkey patching dict will work for dict literals 
> too.
> 
> Also, this should work with scalars,  'a string' be translated in what 
> actually is produced by str('a string') (of course the internal code for 
> building scalars should still be there).
> 
> If that is feasible without big refactoring and do not introduce noticeable 
> performance loss is what I don't know, but it could be a nice feature of 
> __builtin__ module IMO (at less I expected it to work like this when I first 
> tried it).
> 
C:\Steve\Projects\tgtest>python
Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on 
win32
Type "help", "copyright", "credits" or "license" for more information.
Started with C:/Steve/.pythonrc
  >>> myString = "This is a string and it always will be"
  >>> oldstr = str
  >>> class mystr(oldstr):
  ...   def __new__(*a, **kw):
  ...     print "called:", a, kw
  ...
  >>> import __builtin__
  >>> __builtin__.str = mystr
  >>>
Readline internal error
Traceback (most recent call last):
   File "C:\Python24\lib\site-packages\readline\Console.py", line 644, 
in hook_wrapper_23
     raise TypeError, 'readline must return a string.'
called: (<class '__main__.mystr'>, <exceptions.TypeError instance at 
0x00B3E2B0>
) {}
called: (<class '__main__.mystr'>, 'TypeError') {}
None
  >>> type(myString)
Readline internal error
Traceback (most recent call last):
   File "C:\Python24\lib\site-packages\readline\Console.py", line 644, 
in hook_wrapper_23
     raise TypeError, 'readline must return a string.'
called: (<class '__main__.mystr'>, <exceptions.TypeError instance at 
0x00B4D288>
) {}
called: (<class '__main__.mystr'>, 'TypeError') {}
None
  >>>

Seems like there might be a few glitches to debug still ... or perhaps 
it's my fault for using readline? Let's try under Cygwin as a program:

sholden at bigboy ~/Projects/Python
$ cat test39.py
myStr = "This is a string and it alwyas will be"

oldstr=str

class mystr(str) :
     def __new__(*a, **kw) :
         print 'called : ', a, kw
         return oldstr.__new__(*a, **kw)

import __builtin__

__builtin__.str = mystr

print type(myStr)
newStr = "This is another string and it always will be"
print type(newStr)
strStr = str(newStr)
print type(strStr)

sholden at bigboy ~/Projects/Python
$ python test39.py
<type 'str'>
<type 'str'>
called :  (<class '__main__.mystr'>, 'This is another string and it 
always will be') {}
<class '__main__.mystr'>

So, what are you trying to say? The type of the literal strings is 
clearly just what it always was. Where, when and how exactly does this 
magical monkey patch affect literals?

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list