0

I have a fundamental problem with a python class that I use in my scripts, that I will try to describe in the following example. Basically, I have one given class, let's call it test:

class test:
    dic={}
    def __init__(self,dic=dic):
        self.dic=dic

To my class is attached a dictionary 'dic'. Sometimes I need to create two objects of that class, and then build their own dictionary separately. But my problem is that even if those two objects have different memory allocation, their dictionaries can have the same memory allocation:

v1=test()
v2=test()
# v1 and v2 objects have different id
print(id(v1),id(v2))
>> 140413717050928 140413717051040

# However, v1 and v2 properties have the same id
v1.dic['hello']=1
v2.dic['hello']=2
print(id(v1.dic['hello']),id(v2.dic['hello']))
>> 94431202597440 94431202597440

So I can not build my two objects separately:

print(v1.dic['hello'],v2.dic['hello'])
>> 2 2

This is a huge problem for me. How can I create two objects of this class that will have separate properties?

ant.kr
  • 39
  • 5
  • Ok, but how can I declare my class property? – ant.kr Mar 29 '20 at 13:29
  • I'm not sure that I really understand what you're trying to do here. If you want to provide a default value, the standard way would be to do `def __init__(self, dic=None):` and `if dic is None: dic = { }`. Create it explicitely here, just don't do `dic = ` if you don't want to have it shared between instances. – Thierry Lathuille Mar 29 '20 at 13:37
  • Yes but I think that it would be the same, because I always declare my class empty – ant.kr Mar 29 '20 at 14:04

1 Answers1

1

Since if I understood what you want correctly, you need your objects to have empty dictionaries that not necessarily contain the same keys, I would define the empty dictionary as an instance attribute under __init__() and add items to each object dictionary according to further needs:

class Test:
    def __init__(self):
        self.dic = {}


if __name__ == '__main__':
    d1 = Test()
    d2 = Test()
    d1.dic['Hello'] = 1
    d2.dic['Hello'] = 2
    print(d1.dic)
    print(d2.dic)

Output:

{'Hello': 1}
{'Hello': 2}
  • Sorry, I dont understand where the class ends – ant.kr Mar 29 '20 at 13:37
  • it ends at line 3 –  Mar 29 '20 at 13:38
  • Ok, but what is the use of that : "if __name__ == '__main__':" ? – ant.kr Mar 29 '20 at 13:40
  • You might be interested in checking this https://stackoverflow.com/questions/419163/what-does-if-name-main-do –  Mar 29 '20 at 13:42
  • Right, thanks. Apart for that, I dont think that I could use your solution for my case. When I call my class, I dont want to define it's properties right away, the dictionnary contains a lot of different keys related to different objects that are not always the same. Ideally, I would like to have independants empty dictionnaries attached to my class. – ant.kr Mar 29 '20 at 13:50
  • I edited my answer, please check it and tell if this solves your problem –  Mar 29 '20 at 13:55
  • Yes it does, thanks! Please could you remove the "if __name__ == '__main__':" ? It also work without this – ant.kr Mar 29 '20 at 14:02
  • the `if __name__ == '__main__'` is included by convention at the end of the script it's not about the code not able to work without it however it's important if you are going to import this module by other modules for not running the script as a whole during imports, if you want to understand what it does, check the link I included above and also you might want to upvote the answer if it were helpful to solving your problem and maybe accept it later if it turns out to be the best answer –  Mar 29 '20 at 14:05
  • No but my class is contained in a separate script, then I use it to build objects in different scripts – ant.kr Mar 29 '20 at 14:07