"has" Operator

Quinn Dunkan quinn at yak.ugcs.caltech.edu
Sun Jul 8 18:47:23 EDT 2001

On Sun, 08 Jul 2001 12:43:13 GMT, Guido van Rossum <guido at python.org> wrote:
>Actually, Paul's proposal to write
>  x has y
>instead of
>  hasattr(x, 'y')
>has a nice Pythonic ring to it, and the fact that you have to use
>quotes with hasattr() has always bothered me.  Too bad I didn't think
>of this 10 years ago; right now, I'd say it's too little value for too
>much disturbance (a new keyword).

Actually, I think there's a nice symmetry to hasattr taking strings.
It is to objects what has_key is to dicts, though it's more important because
dicts are naturally keyed with strings while objects are naturally keyed with

Now, the spelling I don't quite like.  We should have either haskey and
hasattr, or has_key and has_attr.  I prefer the latter, but it's too late now
in any case.

'x has y' would also have a nice resemblance to 'x in y', where one checks if
y is in the object itself, and the other checks if y is in the container the
object represents.  I don't want the former functionality often enough to
think a keyword is worth it, though.

More information about the Python-list mailing list