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
  Functions: why use a variable "display" in a UDF as a Boolean MMartin71 3 399 May-26-2025, 05:55 AM
Last Post: DeaD_EyE
  PYTHONHOME Variable correct setting msetzerii 0 247 May-25-2025, 11:48 PM
Last Post: msetzerii
  how to get variable Azdaghost 3 619 Apr-23-2025, 07:43 PM
Last Post: deanhystad
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 378 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 750 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  creating arbitrary local variable names Skaperen 9 2,145 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 3,235 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  how solve: local variable referenced before assignment ? trix 5 1,976 Jun-15-2024, 07:15 PM
Last Post: trix
  Variable being erased inside of if statement deusablutum 8 2,421 Jun-15-2024, 07:00 PM
Last Post: ndc85430
  Cant contain variable in regex robertkwild 3 1,257 Jun-12-2024, 11:50 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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