Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Overriding builtin types

Thread Tools

Overriding builtin types

Lonnie Princehouse
Posts: n/a
Dear gurus-at-large,

Does anyone know if there's a mechanism to make the interpreter use
user-defined classes for interpreting literals, instead of the builtin

For instance, wouldn't it be cool if the following code raised an

class IllegalValue(Exception): pass

class NaturalNumber(int):
base_init = int.__init__
one = 1
def __init__(self, value):
NaturalNumber.base_init(self, value)
if self <
raise IllegalValue = NaturalNumber

print -1

I realize that there are other ways to do this, e.g. print
NaturalNumber(-1), but the above just seems like it would be much more

Right now the only way I can see of doing it is to tack on a
pre-interpreter that will substitute LITERAL ->
ArbitraryFunction(LITERAL) implicitly, but that's a lot of effort and
hardly efficient...

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
subtle error slows code by 10x (builtin sum()) - replace builtin sumwithout using import? bdb112 Python 2 07-02-2011 03:13 AM
sub-classing the types in the builtin module datetime Colin J. Williams Python 1 08-15-2007 05:03 PM
Pointer to operator for builtin types =?iso-8859-1?q?Erik_Wikstr=F6m?= C++ 6 12-15-2006 11:39 AM
Overriding builtin getattr method Raja Raman Sundararajan Python 4 10-03-2006 12:06 PM
subclassing builtin types =?ISO-8859-1?Q?BJ=F6rn_Lindqvist?= Python 2 10-08-2004 07:39 AM