Skip to content

Instantly share code, notes, and snippets.

@etscrivner
Created June 11, 2015 21:00
Show Gist options
  • Select an option

  • Save etscrivner/cda3d801c6ecb3d8452b to your computer and use it in GitHub Desktop.

Select an option

Save etscrivner/cda3d801c6ecb3d8452b to your computer and use it in GitHub Desktop.
How to fix the descriptor issue.
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