Python Forum
Class variable / instance variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class variable / instance variable
#1
class Something:

    saved_data = [2, 3, 4, 5]

    def __init__(self):
        self.data = Something.saved_data
        self.data2 = [x for x in Something.saved_data]

x = Something()

print(x.data is Something.saved_data)
print(x.data2 is Something.saved_data)
Very basic question:
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?

self.data2 is a solution, but is there a better/faster/pythonic way?

Thank you,
ifigazsi
Reply
#2
(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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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
#4
Something like this ?

class Something:
 
    data = [2, 3, 4, 5]
 
    def __init__(self):
        self.reset_data()

    def reset_data(self):
        self.data = Something.data[:]
 
something = Something()
 
something.data.append(6)
print(Something.data, something.data)
something.reset_data()
print(Something.data, something.data)
Output:
[2, 3, 4, 5] [2, 3, 4, 5, 6] [2, 3, 4, 5] [2, 3, 4, 5]
Reply
#5
(Jul-26-2020, 04:03 PM)Yoriz Wrote: Something like this ?

class Something:
 
    data = [2, 3, 4, 5]
 
    def __init__(self):
        self.reset_data()

    def reset_data(self):
        self.data = Something.data[:]
 
something = Something()
 
something.data.append(6)
print(Something.data, something.data)
something.reset_data()
print(Something.data, something.data)
Output:
[2, 3, 4, 5] [2, 3, 4, 5, 6] [2, 3, 4, 5] [2, 3, 4, 5]


Yes, thank you! :)
But is this a bad design or not necessarily?
Reply
#6
Good or bad design depends on the problem you are trying to solve. Global variables are usually bad, but sometimes they can lead to an elegant solution for the right kind of problems. Using a class with no instances is usually bad, but it can be an elegant way to implement a singleton.

With no information about your problem it is impossible to say if any design is good or bad.
Reply
#7
(Jul-26-2020, 05:43 PM)deanhystad Wrote: Good or bad design depends on the problem you are trying to solve. Global variables are usually bad, but sometimes they can lead to an elegant solution for the right kind of problems. Using a class with no instances is usually bad, but it can be an elegant way to implement a singleton.

With no information about your problem it is impossible to say if any design is good or bad.

Ok, let's assume you have a few workshops, you want to track the component list and sometimes they got a refill. (i know it's simplistic, but for now that's all i need)
Is it a good/bad design? How you would improve this?

class Workshop:

    def __init__(self, name):
        self.name = name
        self.components_data = ['d2', 'f3', 'g4', 'h5']
        self.refill_all_components()

    def refill_all_components(self):
        self.components_available = self.components_data[:]
        self.components_used = []

    def use_a_component(self):
        from random import shuffle
        shuffle(self.components_available)
        self.components_used.append(self.components_available.pop())

    def __repr__(self):
        return (f'{self.name} components left: {self.components_available} components used: {self.components_used}')

workshop1 = Workshop('Unit1')
workshop2 = Workshop('Unit2')
workshop3 = Workshop('Unit3')

for i in range(2):
    workshop1.use_a_component()
print(workshop1)
workshop1.refill_all_components()
print(workshop1)
Reply
#8
It is not a common practice to hid import statements inside a function. They should all appear at the top of the file unless you are trying to get around a circular reference problem.

As I said before, you cannot judge design in a vacuum. If this design works well for solving your problem, it is a good design. I can say that is is not a generic solution. This would never be a module that others can use to solve their problem. Components should be generic. An inventory control system would never have a "refill_all_components" method. It might have method(s) to check inventory numbers and aid in ordering, but "get everything right now" is unrealistic. In an inventory system the components would be more than just a string, they would be objects. The inventory system would know how to communicate with objects to let you know if a component was available or when a component was available.

Is this a good design? Maybe yes. Maybe no.
Reply
#9
(Jul-27-2020, 02:57 PM)deanhystad Wrote: It is not a common practice to hid import statements inside a function. They should all appear at the top of the file unless you are trying to get around a circular reference problem.

As I said before, you cannot judge design in a vacuum. If this design works well for solving your problem, it is a good design. I can say that is is not a generic solution. This would never be a module that others can use to solve their problem. Components should be generic. An inventory control system would never have a "refill_all_components" method. It might have method(s) to check inventory numbers and aid in ordering, but "get everything right now" is unrealistic. In an inventory system the components would be more than just a string, they would be objects. The inventory system would know how to communicate with objects to let you know if a component was available or when a component was available.

Is this a good design? Maybe yes. Maybe no.

Thanks that make sense. Anyway next time, I try to design something more generic and reusable. (On the other hand I guess that means more codeing)
Reply
#10
It's worth to mention that probably in real life one would like to have a count of the different components on their inventory
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 173 10 hours ago
Last Post: deanhystad
  Commas issue in variable ddahlman 6 383 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 198 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 172 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 251 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 251 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 491 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 633 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 430 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  working directory if using windows path-variable chitarup 2 724 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