Questions about subclassing an int

Discussion in 'Python' started by Steven W. Orr, Jan 4, 2008.

  1. class S(int):
    def __init__(self, value):
    self.value = value
    def addStr(self, str):
    self.doc = str

    s = S(44)
    s.addStr('Hello')

    print 's = ', s
    print 's.doc = ', s.doc

    class T(int):
    def __init__(self, value, str):
    self.value = value
    self.doc = str

    t = T(44, 'Goodbye')

    print 't = ', t
    print 't.doc = ', t.doc

    It works ok with S but it fails when I try to instantiate T with a syntax
    error. Why?

    Also, I don't understand why S works. If I change the name of value and
    use something else, the print of s still works by printing the integer
    value out. How does it know what value to use? Also, in S.__init__, should
    I be calling super(S, self).__init__(value) or is there a difference?

    And just for fun:

    class R(int):
    def __init__(self, value, doc):
    super(R, self).__init__(value)
    self.doc = doc

    r = R(66,'GGG')
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    TypeError: an integer is required

    Now it's no longer a syntax error but I don't see why it's different?

    --
    Time flies like the wind. Fruit flies like a banana. Stranger things have .0.
    happened but none stranger than this. Does your driver's license say Organ ..0
    Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
    individuals! What if this weren't a hypothetical question?
    steveo at syslang.net
     
    Steven W. Orr, Jan 4, 2008
    #1
    1. Advertising

  2. On Jan 4, 10:55 pm, "Steven W. Orr" <> wrote:
    > class S(int):
    >      def __init__(self, value):
    >         self.value = value
    >      def addStr(self, str):
    >         self.doc = str
    >
    > s = S(44)
    > s.addStr('Hello')
    >
    > print 's = ', s
    > print 's.doc = ', s.doc
    >
    > class T(int):
    >      def __init__(self, value, str):
    >         self.value = value
    >         self.doc = str
    >
    > t = T(44, 'Goodbye')
    >
    > print 't = ', t
    > print 't.doc = ', t.doc
    >
    > It works ok with S but it fails when I try to instantiate T with a syntax
    > error. Why?
    >
    > Also, I don't understand why S works. If I change the name of value and
    > use something else, the print of s still works by printing the integer
    > value out. How does it know what value to use? Also, in S.__init__, should
    > I be calling super(S, self).__init__(value) or is there a difference?


    I suggest you read http://www.python.org/download/releases/2.2.3/descrintro/

    Briefly, S(44) calls first S.__new__(S, 44) which does not exist, so
    falls back to int.__new__(S, 44) which creates the new object s which
    is 44 as an integer. Then *only*, s.__init__(44) is called and your
    code is executed. Try changing self.value=value to self.value='SPAM',
    you will see that 'print s' still returns 44.

    As for T(44, 'Goodbye'), the same happens. First T.__new__ does not
    exist, so int.__new__(T, 44, 'Goodbye') is executed and this is where
    the error comes from (shouldn't it be a TypeError?) as this means
    "create the integer whose representation in base 'Goodbye' is 44".

    As for calling int.__init__, there is no point: integers don't have an
    __init__() since they are immutable.

    > And just for fun:
    >
    > class R(int):
    >      def __init__(self, value, doc):
    >          super(R, self).__init__(value)
    >          self.doc = doc
    >
    > r = R(66,'GGG')
    > Traceback (most recent call last):
    >    File "<stdin>", line 1, in ?
    > TypeError: an integer is required
    >
    > Now it's no longer a syntax error but I don't see why it's different?


    Same as above, though I don't understand why you get a SyntaxError for
    T and a TypeError for R. AFAICT both shoult give a TypeError.

    --
    Arnaud
     
    Arnaud Delobelle, Jan 4, 2008
    #2
    1. Advertising

  3. On Fri, 04 Jan 2008 15:36:27 -0800, Arnaud Delobelle wrote:

    > > Now it's no longer a syntax error but I don't see why it's different?

    >
    > Same as above, though I don't understand why you get a SyntaxError for T
    > and a TypeError for R. AFAICT both shoult give a TypeError.


    Probably because it was never a SyntaxError in the first place. If you
    execute the code given for T, it gives a TypeError, just as you would
    expect.

    Possibly the Original Poster had mistyped something at some point and got
    a SyntaxError, or more likely he's just using "syntax error" to mean
    "some exception which I haven't actually looked at".


    --
    Steven
     
    Steven D'Aprano, Jan 5, 2008
    #3
    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. Schnoffos
    Replies:
    2
    Views:
    1,222
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,649
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    460
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    958
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    436
    James Kanze
    Apr 17, 2007
Loading...

Share This Page