Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 problem with simple class code
#1
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?
snippsat wrote Jan-26-2018, 01:21 AM:
Please post all code, output and errors (it it's entirety) between their respective tags.
Refer to BBCode help topic on how to post.
Quote
#2
Please post your code in Python code tags. You can find help here.
Quote
#3
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.
Quote
#4
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.
Quote
#5
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.
Quote
#6
nice,thanks for the alt solution!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Class Problem scratchmyhead 3 88 Nov-19-2019, 08:28 AM
Last Post: Larz60+
  Simple code works in Jupyter but not VS Code Matt_O 2 79 Nov-17-2019, 01:15 AM
Last Post: Matt_O
  Simple problem. looking for an efficient way silverchicken24 3 203 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  help with simple LOGIN code pedrosa06 3 286 Jun-27-2019, 03:58 PM
Last Post: ichabod801
  simple string & input problem kungshamji 5 359 Jun-23-2019, 03:54 PM
Last Post: kungshamji
  Can someone please help me convert this simple C ROT cipher code to Python code? boohoo9 5 354 Jun-14-2019, 03:02 PM
Last Post: DeaD_EyE
  Need help understanding simple Array code. Please. stluwa 1 287 Apr-13-2019, 07:16 PM
Last Post: loomski
  problem with class method AmirAB 3 377 Feb-13-2019, 01:51 AM
Last Post: AmirAB
  A simple csv code Truman 9 807 Dec-22-2018, 01:16 PM
Last Post: gehrenfeld
  simple register code aocii 2 428 Dec-22-2018, 11:15 AM
Last Post: aocii

Forum Jump:


Users browsing this thread: 1 Guest(s)