[Tutor] Inherit from int?

Marilyn Davis marilyn at deliberate.com
Sun May 13 01:31:41 CEST 2007


Hello Tutors,

I'm stumped.  This silly bit of code doesn't work.  I expect the
output to be 8, not 18.  What am I missing?

#!/usr/bin/env python
'''An Under10 class, just to fiddle with inheriting from int.'''

class Under10(int):

    def __init__(self, number):
        number %= 10
        int.__init__(self, number)

if __name__ == '__main__':
    n = Under10(18)
    print n

'''
$ ./new_style.py
18
'''

Any ideas?

Thank you.

Marilyn Davis




More information about the Tutor mailing list