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
  simple login problem MMOToaster 2 203 Feb-25-2020, 09:28 AM
Last Post: MMOToaster
  Class problem duckduck23 2 101 Feb-10-2020, 08:52 PM
Last Post: jefsummers
  Class code problem from CS Dojo YouTube Dixon 3 187 Feb-04-2020, 10:23 PM
Last Post: snippsat
  Generate simple melodic line from code QuarterMissiv 7 234 Jan-09-2020, 05:27 PM
Last Post: QuarterMissiv
  I am a newbie.Help me with this simple piece of code feynarun 3 167 Jan-08-2020, 12:40 PM
Last Post: perfringo
  Problem with simple 3D Vektor calculation Pythocodras 0 91 Dec-11-2019, 07:18 PM
Last Post: Pythocodras
  Class Problem scratchmyhead 3 196 Nov-19-2019, 08:28 AM
Last Post: Larz60+
  Simple code works in Jupyter but not VS Code Matt_O 2 178 Nov-17-2019, 01:15 AM
Last Post: Matt_O
  Simple problem. looking for an efficient way silverchicken24 3 290 Oct-14-2019, 07:13 PM
Last Post: Larz60+
  help with simple LOGIN code pedrosa06 3 352 Jun-27-2019, 03:58 PM
Last Post: ichabod801

Forum Jump:


Users browsing this thread: 1 Guest(s)