#!/usr/bin/env python
#-*- coding: utf-8 -*-
#from fonctions import ObjectFactory
class ObjectFactory():
def __init__(self, valueA) :
self.valueA = valueA
list_objects = []
list_objects.append(ObjectFactory( 'Without variable'))
valueA = '1st value via variable'
list_objects.append(ObjectFactory( valueA))
valueA = '2nd value via variable'
list_objects.append(ObjectFactory( valueA))
dict_value = {}
dict_value['firstEntry'] = 'value unchanged'
dict_value['secondEntry'] = '1st value via dict'
list_objects.append(ObjectFactory( dict_value))
dict_value['secondEntry'] = '2nd value via dict'
list_objects.append(ObjectFactory( dict_value))
for my_object in list_objects :
#print my_object
print my_object.valueA
Hi, I don't understand the behavior of how the object is being built. Executing the code gives the following result :
Without variable
1st value via variable
2nd value via variable
{'firstEntry': 'value unchanged', 'secondEntry': '2nd value via dict'}
{'firstEntry': 'value unchanged', 'secondEntry': '2nd value via dict'}
But I was thinking it would give me (line 4 displays '1st' instead of '2nd' )
Without variable
1st value via variable
2nd value via variable
{'firstEntry': 'value unchanged', 'secondEntry': '1st value via dict'}
{'firstEntry': 'value unchanged', 'secondEntry': '2nd value via dict'}
As you can see, if the value is passed by a variable, it's the variable content at the time of building the object that is taking into account. But with a dictionnary, it's the last value being affected and not the value affected when the objet is being built.
Can someone explain what is happening ?