3

Let say I have this code:

for obj in self.objects:
  template = obj
  # Exact values are only numbers (float, int) and or strings
  dct = {
    'a': template.id,
    'b': template.some_other_value,
    'c': template.some_other_value2,
  }
  some_other_obj.use_dct(dct) # Do something with it
  for obj2 in obj:
    # reuse same dictionary, 
    #but regenerate values from another object
    template = obj2 
    some_other_obj.use_dct(dct)

Now doing this, old values are kept and obj2 gets same dictionary with same values as obj. Is it possible to somehow regenerate that dictionary so everything would be the same, except template would point to another object and would "regenerate" values from that another object? Or do I have to manually specify another dictionary with same keys and fill it with another object?

Andrius
  • 19,658
  • 37
  • 143
  • 243
  • So, to make it clear, you expect `dct['a']` to magically update from `obj.id` to `obj2.id` when you write `template = obj2`? – 301_Moved_Permanently Jul 07 '15 at 08:17
  • @MathiasEttinger Of course not, I was just making point with `template = obj2`, so people would understand what I'm trying to do. All I'm asking, if there is any pattern that would allow to reproduce something similar, so you would not need to rewrite same dictionary twice, but regenerate values dynamically. How would you show example of code you need, but actually don't know how that code should look like? Thats the point of the question you know. – Andrius Jul 07 '15 at 08:26

2 Answers2

0

You can't re-assign values to a dict without explicitly assigning them.

What you can do is either write a function that will do it for you each time you need it:

def update_dict_from_obj(dct, obj):
  dct.update({
    'a': obj.id,
    'b': obj.some_other_value,
    'c': obj.some_other_value2,
  })

for obj in self.objects:
  # Exact values are only numbers (float, int) and or strings
  dct = {}
  update_dict_from_obj(dct, obj)
  some_other_obj.use_dct(dct) # Do something with it
  for obj2 in obj:
    # reuse same dictionary, 
    #but regenerate values from another object
    update_dict_from_obj(dct, obj2)
    some_other_obj.use_dct(dct)

Or you can rewrite some_other_obj.use_dct to accept your object instead of a dictionnary

301_Moved_Permanently
  • 4,007
  • 14
  • 28
0

Looks to me like you're trying to use a dictionary, when what you really want is something else entirely.

That "something" should store a reference to an object, then return properties of that object using names other than the names of the properties. (This seems to be a key requirement, otherwise you'd just use obj itself, right?)

So why not make a class that does what you want it to do?

class AbcClass:
    def __init__(self, in_object):
        self.obj = in_object

    def dictionary(self):
        obj = self.obj
        return dict(a = obj['id'], b = obj['foo'], c = obj['bar'])

def use_dct(dct):
    print "ID: %s" % dct['a']

objects = [
        dict(id = 1, foo = 'foo', bar = 'bar'),
        dict(id = 2, foo = 'baz', bar = 'qux')
        ]

for obj in objects:
    use_dct(AbcClass(obj).dictionary())

This produces the output:

ID: 1
ID: 2

You could also get seriously fancy, and make the object itself into an iterator. (But that might be surplus to requirements.)

Community
  • 1
  • 1
LondonRob
  • 73,083
  • 37
  • 144
  • 201