A
A
AFANASIY_THE_CAT2018-08-22 08:46:45
Python
AFANASIY_THE_CAT, 2018-08-22 08:46:45

Static vocabulary in the classroom. How to fix?

class Person():
    member = {"lleg" : 2,"rleg" : 2,"larm" : 2,"rarm" : 2}
hero = Person()
enemy = Person()

hero.member["lleg"] = 1
enemy.member["larm"] = 1

print (str(hero.member["larm"]) + " " + str(enemy.member["lleg"])) # 1 1

Such a problem. I create two instances of the class in which the list is present. By changing the dictionary in one instance, the dictionary is changed in all other instances. I understand that the dictionary has become static by default. How can I make sure that each instance of this class has its own values ​​in the dictionary?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JaxxDexx, 2018-08-22
@AFANASIY_THE_CAT

class Person():
    def __init__(self):
        self.member = {"lleg" : 2,"rleg" : 2,"larm" : 2,"rarm" : 2}

hero = Person()
enemy = Person()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question