default behavior

Christian Heimes lists at cheimes.de
Thu Jul 29 15:40:16 EDT 2010


> Inheriting from "int" is not too helpful, because you can't assign
> to the value of the base class.  "self=1" won't do what you want.

It's useful if you remember that you can set the default value by
overwriting __new__.

>>> class int1(int):
...     def __new__(cls, value=1):
...         return super(int1, cls).__new__(cls, value)
...
>>> int1()
1
>>> int1(1)
1
>>> int1(2)
2
>>> int1(0)
0




More information about the Python-list mailing list