Subclass str: where is the problem?
Peter Otten
__peter__ at web.de
Mon Apr 24 10:03:16 EDT 2006
pascal.parent at free.fr wrote:
> This is good... try this:
>
> value = 'test'
> value
> 'test'
> type(value)
> <type 'str'>
> value = type(value) is type('') and Upper(value) or value
> value
> 'TEST'
> type(value)
> <class '__main__.Upper'>
Try again with
value = ""
This makes Upper(value) a False value in a boolean context:
>>> class Upper(str):
... pass
...
>>> Upper("")
''
>>> type(Upper(""))
<class '__main__.Upper'>
>>> type(Upper("") or "")
<type 'str'>
versus
>>> type(Upper("x") or "x")
<class '__main__.Upper'>
Peter
More information about the Python-list
mailing list