Dictionary inheritance

Jordan Rastrick jrastrick at student.usyd.edu.au
Sat Aug 13 01:21:47 EDT 2005


Talin asked:

> Also, on a completely different subject: Has there been much discussion
> about extending the use of the 'is' keyword to do type comparisons a la
> C# (e.g. "if x is list:") ?
>
> -- Talin

No, is already has a specific, well defined meaning - object identity.

IDLE 1.1
>>> a = [1,2,3]
>>> a is list
False
>>> b = type(a)
>>> b
<type 'list'>
>>> b is list
True

"Extending it" to mean something entirely different to what it
currently means is a bad idea, and is also unnessecary - the builtin
function isinstance already provides the functionaliy you're looking
for:

>>> isinstance(b, list)
False
>>> isinstance(a, list)
True
>>>

However, use sparingly - calling isinstance unnessecarily rather than
relying on polymorphism is considered pretty unpythonic, and usually
reflects pretty poor OO design.




More information about the Python-list mailing list