[PATCH 2 of 3 RFC] Remove all uses of hasattr(a, b) in favor of getattr(a, b, None)

Matt Mackall mpm at selenic.com
Mon Mar 14 12:48:18 CDT 2011


On Sun, 2011-03-13 at 12:11 +0100, Benoit Boissinot wrote:
> On Tue, Mar 8, 2011 at 9:36 AM, Martin Geisler <mg at aragost.com> wrote:
> > Augie Fackler <durin42 at gmail.com> writes:
> >
> >> On Mar 3, 2011, at 8:03 AM, Michael Haggerty wrote:
> >>
> >>> For the other cases, maybe it would make sense to define a
> >>>
> >>> def safe_hasattr(o, name):
> >>>    try:
> >>>        getattr(o, name)
> >>>    except AttributeError:
> >>>        return False
> >>>    else:
> >>>        return True
> >>
> >> Does anyone in the wider mercurial-devel have a feeling on this one?
> >> It seems reasonable as a hedge against the day in about 2050 that we
> >> can be Python3 only...
> 
> It seems it could be useful? Can you respin the patch with
> safe_hasattr() and the "optimized defaults"?
> >
> > Would it not make sense to overwrite __builtin__.hasattr with such a
> > safe version or is that considered very bad style?
> >
> 
> Dunno, I don't think we monkey-patch python internals already, do we?

We do, but sparingly (see demandimport). In WSGI environments and
elsewhere, messing with globals can be problematic.

-- 
Mathematics is the supreme nostalgia of our time.




More information about the Mercurial-devel mailing list