0

I'm new to python and have a problem I can't figure out.

I have a Network class and a Person class in a file called practice

import uuid

class Network:
_people = []

def create_person(self):
    person = Person()
    self._people.append(person)
    return person

def add_person_property(self, person, prop, value):
    index = self._people.index(person)
    self._people[index].add_property(prop, value)


class Person:
_prop = {}
_relations = {}

def __init__(self):
    self.pid = uuid.uuid4()

def add_property(self, prop, value):
    self._prop.update({prop: value})

I also have another file for testing where the above gets imported

from practice import *

n = Network()
p1 = n.create_person()
p2 = n.create_person()
p3 = n.create_person()

n.add_person_property(p1, 'name', 'tom')
n.add_person_property(p3, 'name', 'john')
n.add_person_property(p2, 'name', 'bob')

Every time n.add_person_property is called, the prop and value given go to every person's _prop dictionary instead of the person object that was specified in the list.

Below is a screenshot of the debug variables after one run through of add_person_property.

enter image description here

0 Answers0