"has" Operator

Guido van Rossum guido at python.org
Sun Jul 8 20:51:12 EDT 2001


quinn at yak.ugcs.caltech.edu (Quinn Dunkan) writes:

> 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 identifiers.

(From the idle speculation department)

The hasattr() function matches getattr() and setattr().  The 'has'
form, if it existed, would match the "." notation: think 'x.foo'
vs. 'x has foo'.

> 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.

Ah, but in 2.2, you will be able to write 'key in dict' for
'dict.has_key(key)'.  Problem solved. :-)

> '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.

Bingo!

--Guido van Rossum (home page: http://www.python.org/~guido/)



More information about the Python-list mailing list