-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetattr_gotcha.py
58 lines (40 loc) · 1.51 KB
/
getattr_gotcha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
'''
getattr_gotcha.py - Unintuitive behaviour of __getattr__ - beware!
I'll fill this with the explanation in once Strawbs has had a chance
to puzzle over it...
Author: Eric Saunders
September 2012
'''
class WorkingDelegatingIterator(object):
'''This simple class is iterable. Iteration is delegated explicitly; everything
else is delegated via __getattr__.'''
def __init__(self):
self.x = [1,2,3]
def __iter__(self):
return self.x.__iter__()
def __getattr__(self, attr):
return getattr(self.x, attr)
class BrokenDelegatingIterator(object):
'''This class quite reasonably tries to delegate everything via __getattr__.
Some things work. So why doesn't iteration?'''
def __init__(self):
self.x = [1,2,3]
def __getattr__(self, attr):
return getattr(self.x, attr)
if __name__ == '__main__':
print "1) Trying iterator with explicitly defined __iter__():"
print "Getting length via __getattr__():"
delegator = WorkingDelegatingIterator()
print "Length:", delegator.__len__()
print "Trying iteration:"
for value in delegator:
print value
print
print "2) Trying iterator with implicitly defined __iter__(), via __getattr__():"
print "Getting length via __getattr__():"
delegator = BrokenDelegatingIterator()
print "Length:", delegator.__len__() # This works...
print "Trying iteration:"
for value in delegator: # So why doesn't this?
print value