Python Forum
problem with simple class code
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?
Reply
#2
Please post your code in Python code tags. You can find help here.
Reply
#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.
Reply
#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.
Reply
#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.
Reply
#6
nice,thanks for the alt solution!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with simple code JacobSkinner 1 230 Mar-18-2024, 08:08 PM
Last Post: deanhystad
  I have a code which is very simple but still I cannot detect what's wrong with it max22 1 440 Nov-07-2023, 04:32 PM
Last Post: snippsat
  A simple problem, how best to solve it? SuchUmami 2 683 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  help me simple code result min and max number abrahimusmaximus 2 871 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,400 Jun-18-2022, 10:59 AM
Last Post: deanhystad
  How to solve this simple problem? Check if cvs first element is the same in each row? thesquid 2 1,189 Jun-14-2022, 08:35 PM
Last Post: thesquid
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,756 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,239 Oct-04-2021, 05:18 PM
Last Post: snippsat
Big Grin question about simple algorithm to my problem jamie_01 1 1,635 Oct-04-2021, 11:55 AM
Last Post: deanhystad
  My simple code don't works !! Nabi666 1 1,577 Sep-06-2021, 12:10 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020