Message241425
| Author |
ionelmc |
| Recipients |
Claudiu.Popa, belopolsky, christian.heimes, ethan.furman, ionelmc, jedwards, llllllllll, terry.reedy |
| Date |
2015年04月18日.16:44:53 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<CANkHFr_n4u-sXGbRxLsp45LS83OA3nyL4j+4HP4NXMCjaytb=w@mail.gmail.com> |
| In-reply-to |
<1429374198.17.0.775176925984.issue23990@psf.upfronthosting.co.za> |
| Content |
On Sat, Apr 18, 2015 at 7:23 PM, Ethan Furman <report@bugs.python.org>
wrote:
>
> class GenericProxy:
> def __init__(self, proxied):
> self.proxied = proxied
> # in case proxied is an __iter__ iterator
> @property
> def __iter__(self):
> if not hasattr(self.proxied, '__iter__'):
> raise AttributeError
> else:
> return self
> @property
> def __next__(self):
> if not hasattr(self.proxied, '__next__'):
> raise AttributeError
> else:
> return next(self.proxied)
Unfortunately your implementation is incorrect as you forgot to that the
property needs to return a function. This is a correct implementation that
works as expected (in the sense that *iter does in fact honor the
descriptor protocol)*:
class GenericProxy:
> def __init__(self, proxied):
> self.proxied = proxied
> # in case proxied is an __iter__ iterator
> @property
> def __iter__(self):
> if not hasattr(self.proxied, '__iter__'):
> raise AttributeError
> else:
> return *lambda:* self
> @property
> def __next__(self):
> if not hasattr(self.proxied, '__next__'):
> raise AttributeError
> else:
> return *lambda: *next(self.proxied)
>
The iter machinery doesn't "grab values and call them", you've
misinterpreted the error.
Thanks,
-- Ionel Cristian Mărieș, http://blog.ionelmc.ro |
|