Python Forum
problem with simple class code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem with simple class code
#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


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 511 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 297 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Help with simple code JacobSkinner 1 359 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 514 Nov-07-2023, 04:32 PM
Last Post: snippsat
  A simple problem, how best to solve it? SuchUmami 2 748 Sep-01-2023, 05:36 AM
Last Post: Pedroski55
  help me simple code result min and max number abrahimusmaximus 2 950 Nov-12-2022, 07:52 AM
Last Post: buran
  Simple encoding code ebolisa 3 1,486 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,268 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,836 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
  Simple code question about lambda and tuples JasPyt 7 3,402 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