0

Here is my code

import json


class Father(object):
    data = {}

    def set_name(self, name):
        self.data['name'] = name

    def dump_data(self):
        print(json.dumps(self.data))

class Brother(Father):

    def __init__(self, name, number_of_cars):
        self.set_name(name)
        self.set_number_of_cars(number_of_cars)

    def set_number_of_cars(self, n):
        self.data['number_of_cars'] = n

class Sister(Father):

    def __init__(self, name, number_of_dolls):
        self.set_name(name)
        self.set_number_of_dolls(number_of_dolls)

    def set_number_of_dolls(self, n):
        self.data['number_of_dolls'] = n


child = Brother('Brother John', 5)
child.dump_data()
child = Sister('Sister Jane', 30)
child.dump_data()

What I expected is

{"name": "Brother John", "number_of_cars": 5}
{"name": "Sister Jane", "number_of_dolls": 30}

But, the result is

{"name": "Brother John", "number_of_cars": 5}
{"number_of_dolls": 30, "name": "Sister Jane", "number_of_cars": 5}

And the result is still the same if I change like this

child1 = Sister('Sister Jane', 30)
child1.dump_data()

I don't know why this issue happens. I thought because I use the data attribute of Father class and data is dictionary (a mutable type in python), but I have 2 different objects of 2 different classes (inherited from one Father class), so that idea does not make sense to me. Where Jane have number_of_cars element? The issue occurs on both Python 2.7.6 and Python 3.4.3.

Please explain me why this happens! Thank you!

Gia Duong Duc Minh
  • 1,319
  • 5
  • 15
  • 30
  • 1
    `data` is a class property, not an instance property. So all instances of the class share the same property. – Barmar Sep 22 '17 at 10:10
  • 1
    You need an `__init__` method in `Father` that does `self.data = {}` so each instance gets it own. – Barmar Sep 22 '17 at 10:11
  • Unless it's just the naming that's a bit misleading here - the use of inheritance here is odd... A `Brother`/`Sister` aren't really `Father`s... rather they *have a `Father`*... You might want to read up on OOP and inheritance before proceeding much further down this path... – Jon Clements Sep 22 '17 at 10:15

1 Answers1

2

The problem is that you are initilizing data as a class property, not a instance property.

You have to move data = {} to Father.__init__() as self.data = {}.

import json


class Father(object):
    def __init__(self):
        self.data = {}

    def set_name(self, name):
        self.data['name'] = name

    def dump_data(self):
        print(json.dumps(self.data))


class Brother(Father):
    def __init__(self, name, number_of_cars):
        super(Brother, self).__init__()
        self.set_name(name)
        self.set_number_of_cars(number_of_cars)

    def set_number_of_cars(self, n):
        self.data['number_of_cars'] = n


class Sister(Father):
    def __init__(self, name, number_of_dolls):
        super(Sister, self).__init__()
        self.set_name(name)
        self.set_number_of_dolls(number_of_dolls)

    def set_number_of_dolls(self, n):
        self.data['number_of_dolls'] = n


child = Brother('Brother John', 5)
child.dump_data()
child = Sister('Sister Jane', 30)
child.dump_data()

And you get the expected result:

{"number_of_cars": 5, "name": "Brother John"}
{"number_of_dolls": 30, "name": "Sister Jane"}
Paco H.
  • 2,034
  • 7
  • 18