Created
June 11, 2015 21:00
-
-
Save etscrivner/cda3d801c6ecb3d8452b to your computer and use it in GitHub Desktop.
How to fix the descriptor issue.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import collections | |
| test = [['A'], ['B'], ['C']] | |
| class ValueDescriptor(object): | |
| def __init__(self): | |
| self.value_store = collections.defaultdict(lambda: None) | |
| def __get__(self, obj, objtype): | |
| return self.value_store[obj] | |
| def __set__(self, obj, value): | |
| self.value_store[obj] = value | |
| class PropItem(object): | |
| value = None | |
| def __init__(self, value): | |
| self.value = value | |
| class DescItem(object): | |
| value = ValueDescriptor() | |
| def __init__(self, value): | |
| self.value = value | |
| prop_results = [PropItem(value=each) for each in test] | |
| desc_results = [DescItem(value=each) for each in test] | |
| print "Expected:", test | |
| print "PropItem:", [each.value for each in prop_results] | |
| print "DescItem:", [each.value for each in desc_results] | |
| # $ python iterfail.py | |
| # Expected: [['A'], ['B'], ['C']] | |
| # PropItem: [['A'], ['B'], ['C']] | |
| # DescItem: [['A'], ['B'], ['C']] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment