Python Forum
Class variable / instance variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class variable / instance variable
#3
(Jul-26-2020, 03:20 PM)buran Wrote:
(Jul-26-2020, 02:32 PM)ifigazsi Wrote: I have a class variable, but I want to change this at instance level, without changing the original class variable, how can i make it
This looks like wrong design - if you want and are going to change it at instance level why it should be class attributee - it should be instance attribute.

Ok, so it seems like wrong design. (first attempt in OOP Cry ) (i tried to update it)

I want to create a initial_data which is accessible by all instances, to start to work with.
Then after a lot of iterations resume to the original (initial) data.

class Something:

    def __init__(self):
        self.saved_data = [2,3,4,5]
        self.data_to_work_with = self.saved_data
        self.data_to_work_with2 = [x for x in self.saved_data]

x = Something()

print(x.data_to_work_with is x.saved_data)
print(x.data_to_work_with2 is x.saved_data)
Reply


Messages In This Thread
Class variable / instance variable - by ifigazsi - Jul-26-2020, 02:32 PM
RE: Class variable / instance variable - by buran - Jul-26-2020, 03:20 PM
RE: Class variable / instance variable - by ifigazsi - Jul-26-2020, 03:40 PM
RE: Class variable / instance variable - by Yoriz - Jul-26-2020, 04:03 PM
RE: Class variable / instance variable - by buran - Jul-28-2020, 11:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 355 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 529 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 290 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 246 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 368 Mar-28-2024, 02:13 PM
Last Post: Larz60+
  optimum chess endgame with D=3 pieces doesn't give an exact moves_to_mate variable max22 1 309 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 548 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 726 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 484 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  working directory if using windows path-variable chitarup 2 776 Nov-28-2023, 11:36 PM
Last Post: chitarup

Forum Jump:

User Panel Messages

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