471,356 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,356 software developers and data experts.

Questions about subclassing an int

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
Jan 4 '08 #1
2 844
On Jan 4, 10:55*pm, "Steven W. Orr" <ste...@syslang.netwrote:
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/relea....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

Jan 4 '08 #2
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
Jan 5 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by WhiteRavenEye | last post: by
4 posts views Thread by GrelEns | last post: by
2 posts views Thread by David Vaughan | last post: by
2 posts views Thread by BJörn Lindqvist | last post: by
3 posts views Thread by Peter Olsen | last post: by
11 posts views Thread by Brent | last post: by
23 posts views Thread by Rick | last post: by
16 posts views Thread by manatlan | last post: by
17 posts views Thread by Donn Ingle | last post: by
5 posts views Thread by Ray | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.