homepage

This issue tracker has been migrated to GitHub , and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: with statement executes type(obj).__exit__ rather than getattr(obj,"__exit__")
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Paul.Wiseman
Priority: normal Keywords:

Created on 2012年09月28日 03:02 by Paul.Wiseman, last changed 2022年04月11日 14:57 by admin. This issue is now closed.

Messages (2)
msg171399 - (view) Author: Paul Wiseman (Paul.Wiseman) Date: 2012年09月28日 03:02
I found this behaviour today and thought it was weird so asked the question on SO here http://stackoverflow.com/questions/12632894/why-doesnt-getattr-work-with-exit/12632972#12632972
basically if I attributes are returned dynamically, they seem to get overlooked by the code that runs the with statements.
>>> class FileHolder(object):
... def __init__(self,*args,**kwargs):
... self.f= file(*args,**kwargs)
... def __getattr__(self,item):
... return getattr(self.f,item)
...
>>> a= FileHolder("a","w")
>>> a.write
<built-in method write of file object at 0x018D75F8>
>>> with a as f:
... print f
...
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AttributeError: __exit__
>>> a.__exit__
<built-in method __exit__ of file object at 0x018D75F8>
msg171401 - (view) Author: Paul Wiseman (Paul.Wiseman) Date: 2012年09月28日 03:10
I got pointed to an explanation in the docs! http://docs.python.org/reference/datamodel.html#special-method-lookup-for-new-style-classes
It was confusing, though :)
History
Date User Action Args
2022年04月11日 14:57:36adminsetgithub: 60272
2012年09月28日 03:10:23Paul.Wisemansetstatus: open -> closed
resolution: not a bug
messages: + msg171401
2012年09月28日 03:02:34Paul.Wisemancreate

AltStyle によって変換されたページ (->オリジナル) /