UserInt module and class

P

pmaupin

Recently I found out the hard way that __coerce__
is not automatically called for new classes.

This means that, at least for me, a 'UserInt' module
is eminently useful, for objects which can be used
in expressions where a numeric result is desired.

Googling for 'python UserInt', I found a few close
things, some far-away things, and a
"next-up-UserInt-ly y'rs - tim" from 1998 when he
was describing why there was no UserString class :)

It occurred to me that others might want UserFloat,
etc. for similar reasons, so I wrote a meta-module
to generate the UserInt module. Perhaps someone
will find it useful.

Best regards,
Patrick Maupin


print """
\"""A more or less complete user-defined wrapper around integer objects.\"""

class UserInt(object):
def __int__(self): raise TypeError, "This is a virtual class"
"""

unwanted_methods = dir(object)+['__int__','__getnewargs__']
methods = [i for i in dir(int) if i not in unwanted_methods]
methods.sort()

for i in methods:
try: getattr(1,i)(1)
except TypeError: params = (i,'',i,'')
else: params = (i,',other',i,'int(other)')
print ' def %s(self%s): return int(self).%s(%s)' % params
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top