[Tutor] reading 2nd line
Magnus Lycka
magnus@thinkware.se
Mon Jan 27 07:50:02 2003
At 17:59 2003-01-27 +0530, Jayprasad J. Hegde wrote:
> a question about file (). Has it been introduced from Python 2.3 onwards?
No, it's in 2.2 as well. It's really part of the type/class
unification. Now you can use type names as constructors.
>>> int('5')
5
>>> int(5)
5
>>> float('5')
5.0
>>> file('c:/autoexec.bat')
<open file 'c:/autoexec.bat', mode 'r' at 0x015C5D98>
You can also subclass types now:
>>> class NonNegInt(int):
... def __init__(self, val):
... if int(val) < 0:
... raise ValueError, 'Negative value!'
... super(int, self).__init__(val)
...
>>> print NonNegInt(5)
5
>>> print NonNegInt(-5)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 4, in __init__
ValueError: Negative value!
>>> print NonNegInt(0)
0
>>> a = NonNegInt(5)
>>> print '*' * a
*****
--
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/ mailto:magnus@thinkware.se