[Python-Dev] 'hasattr' is broken by design (original) (raw)
Michael Foord fuzzyman at voidspace.org.uk
Wed Sep 1 15:26:40 CEST 2010
- Previous message: [Python-Dev] Working on the Python code-base with a VIM-based setup
- Next message: [Python-Dev] 'hasattr' is broken by design
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 24/08/2010 08:40, Michael Foord wrote:
On 24/08/2010 01:25, Nick Coghlan wrote:
On Tue, Aug 24, 2010 at 8:15 AM, Nick Coghlan<ncoghlan at gmail.com> wrote:
Now, it may be worth considering an addition to the inspect module that was basically:
def getattrstatic(obj, attr): """Retrieve attributes without triggering dynamic lookup via the descriptor protocol, getattr or getattribute. Note: this function may not be able to retrieve all attributes reported by dir(obj) """ try: instancedict = object.getattribute(obj, "dict") except AttributeError: pass else: if attr in instancedict: return instancedict[attr] for entry in getmro(obj.class): try: return entry.dict[attr] except AttributeError: pass Second attempt with a default value parameter and correctly raising AttributeError if not found: sentinel = object() def getattrstatic(obj, attr, default=sentinel): """Retrieve attributes without triggering dynamic lookup via the descriptor protocol, getattr or getattribute. Note: this function may not be able to retrieve all attributes reported by dir(obj) """ try: instancedict = object.getattribute(obj, "dict") except AttributeError: pass else: if attr in instancedict: return instancedict[attr] for entry in getmro(obj.class): try: return entry.dict[attr] except AttributeError: pass if default is not sentinel: return default raise AttributeError(attr) This doesn't correctly handle the case where obj is a type object or obj uses slots. If I have time (currently on vacation with only intermittent internet access) I'll provide an update.
I managed an updated version (with tests) during the flight home last night. I've attached it to issue 9732, along with a discussion of the caveats and ways of breaking it:
http://bugs.python.org/issue9732
Michael
Michael
Cheers, Nick.
-- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/blog
READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.
- Previous message: [Python-Dev] Working on the Python code-base with a VIM-based setup
- Next message: [Python-Dev] 'hasattr' is broken by design
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]