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


Messages In This Thread
problem with simple class code - by diegoraffo - Jan-25-2018, 07:35 PM
RE: problem with simple class code - by j.crater - Jan-25-2018, 07:38 PM
RE: problem with simple class code - by league55 - Jan-25-2018, 08:49 PM
RE: problem with simple class code - by ka06059 - Jan-27-2018, 12:15 AM
RE: problem with simple class code - by snippsat - Jan-27-2018, 12:54 AM
RE: problem with simple class code - by ka06059 - Jan-27-2018, 02:31 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How does this code create a class? Pedroski55 6 399 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 265 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Help with simple code JacobSkinner 1 334 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 488 Nov-07-2023, 04:32 PM
Last Post: snippsat
  A simple problem, how best to solve it? SuchUmami 2 728 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  help me simple code result min and max number abrahimusmaximus 2 914 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,456 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,234 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,815 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,337 Oct-04-2021, 05:18 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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