UserInt module and class

Discussion in 'Python' started by pmaupin@speakeasy.net, Oct 14, 2003.

  1. Guest

    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
     
    , Oct 14, 2003
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,785
    Thomas Weidenfeller
    Oct 12, 2005
  2. Maric Michaud
    Replies:
    0
    Views:
    7,194
    Maric Michaud
    Jun 24, 2006
  3. Uncutstone Wu
    Replies:
    9
    Views:
    167
    uncutstone
    Sep 24, 2006
  4. Iñaki Baz Castillo
    Replies:
    3
    Views:
    154
    Iñaki Baz Castillo
    May 2, 2008
  5. Jonno
    Replies:
    0
    Views:
    98
    Jonno
    Feb 19, 2014
Loading...

Share This Page