"has" Operator

Guido van Rossum guido at python.org
Sun Jul 8 14:43:13 CEST 2001

"John Roth" <johnroth at ameritech.net> writes:

> "Paul Sidorsky" <paulsid at home.com> wrote in message
> news:mailman.994543873.6493.python-list at python.org...
> > Roman Suzi wrote:
> >
> > > if hasattr(myobject, "someattribute"):
> > >      foo()
> > > else:
> > >      bar()
> > > is clean enough, IMHO...
> >
> > Oops, I didn't know about hasattr().  Yes, that certainly does the
> > trick.  Though I have to say the syntax is a little odd (what with the
> > quotes), but it's certainly sufficient.  Thanks!  Sorry to bother
> > everyone.
> Not a bother. We learn by watching everyone else ask questions! However,
> the syntax is necessary, since "hasattr" is essentially a dictionary lookup.

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

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

More information about the Python-list mailing list