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
  I trying to automate the Variable Logon button using the python code but I couldn't surendrasamudrala 0 282 Mar-07-2025, 05:02 AM
Last Post: surendrasamudrala
  not able to call the variable inside the if/elif function mareeswaran 3 557 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  creating arbitrary local variable names Skaperen 9 1,817 Sep-07-2024, 12:12 AM
Last Post: Skaperen
  Variable Substitution call keys Bobbee 15 2,756 Aug-28-2024, 01:52 PM
Last Post: Bobbee
  how solve: local variable referenced before assignment ? trix 5 1,747 Jun-15-2024, 07:15 PM
Last Post: trix
  Variable being erased inside of if statement deusablutum 8 2,126 Jun-15-2024, 07:00 PM
Last Post: ndc85430
  Cant contain variable in regex robertkwild 3 1,093 Jun-12-2024, 11:50 AM
Last Post: deanhystad
  is this a valid variable name? Skaperen 6 1,685 Jun-05-2024, 10:13 PM
Last Post: Skaperen
  Help with writing monitored data to mysql upon change of one particular variable donottrackmymetadata 3 1,423 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 1,751 Apr-05-2024, 03:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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