Python Forum
problem with simple class code - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: problem with simple class code (/thread-7803.html)



problem with simple class code - diegoraffo - Jan-25-2018

I'm havingg a problem with my code, I dont know why my code doesn't work. It works well when I use a string value to the condition variable, but not when I try to change a list.
Here is a copy of an example that works:
class Car(object):
    condition = "new"
    def __init__(self, color):
        self.color = color
    def drive(self):
        self.condition = "used"

chevy = Car("Blue")
print (chevy.condition) 
ford = Car("Red")
print (ford.condition)
chevy.drive()
print (chevy.condition)
print (ford.condition)
Result:
Output:
new new used new
The ford condition is new as it shouldbe considerning the ford.drive() wasn't executed.
Now when i change the condition value for a list it doesnt work:
class Car(object):
    condition = ["0","0"]
    def __init__(self, color):
        self.color = color
    def drive(self):
        self.condition[0] = "1"

chevy = Car("Blue")
print (chevy.condition)
ford = Car("Red")
print (ford.condition)
chevy.drive()
print (chevy.condition)
print (ford.condition)
Output:
['0', '0'] ['0', '0'] ['1', '0'] ['1', '0']
In this example the ford condition change despite the ford.drive()has not been executed.
Does anyone know why this happens?


RE: problem with simple class code - j.crater - Jan-25-2018

Please post your code in Python code tags. You can find help here.


RE: problem with simple class code - league55 - Jan-25-2018

Attribute condition is defined before the initialization, so it's an attribute of the class itself and not of the class objects. When you modify the value of that attribute it therefore modifies it for all objects.


RE: problem with simple class code - ka06059 - Jan-27-2018

class Car(object):
    condition = ["0","0"]
    def __init__(self, color):
        self.color = color
    def drive(self):
        clone = self.condition[:]
        clone[0] = '1'
        self.condition = clone
try clone/make a copy of self.condition, make changes on that clone and ressign instead of mutating it.


RE: problem with simple class code - snippsat - Jan-27-2018

There is a easier solution than starting clone/copy stuff @ka06059.
class Car:
    def __init__(self, color):
        self.color = color
        self.condition = ["0","0"]

    def drive(self):
        self.condition[0] = "1"
Test.
>>> chevy = Car("Blue")
>>> ford = Car("Red")
>>> 
>>> # So now setting condition will only affect one object
>>> chevy.drive()
>>> chevy.condition
['1', '0']
>>> ford.condition
['0', '0']
>>> 
Orgianl post with class attribute:
Class attribute are owned by the class itself,so the attribute has the same value for each instance(object) of a particular class.

What i show are instance attribute:
Instance attributes are owned by the specific instance(object) of the class,so can vary from instance to instance of a specific class.


RE: problem with simple class code - ka06059 - Jan-27-2018

nice,thanks for the alt solution!