Python Forum
Class variable / instance variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class variable / instance variable
#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


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 Yoriz - Jul-26-2020, 04:03 PM
RE: Class variable / instance variable - by ifigazsi - Jul-26-2020, 07:04 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 349 Apr-18-2024, 09:55 PM
Last Post: deanhystad
  Commas issue in variable ddahlman 6 515 Apr-05-2024, 03:45 PM
Last Post: deanhystad
  Variable Explorer in spyder driesdep 1 285 Apr-02-2024, 06:50 AM
Last Post: paul18fr
  Mediapipe. Not picking up second variable stevolution2024 1 236 Mar-31-2024, 05:56 PM
Last Post: stevolution2024
Question Variable not defined even though it is CoderMerv 3 358 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 306 Mar-21-2024, 09:31 PM
Last Post: max22
  unbounded variable akbarza 3 527 Feb-07-2024, 03:51 PM
Last Post: deanhystad
  Variable for the value element in the index function?? Learner1 8 721 Jan-20-2024, 09:20 PM
Last Post: Learner1
  Variable definitions inside loop / could be better? gugarciap 2 481 Jan-09-2024, 11:11 PM
Last Post: deanhystad
  working directory if using windows path-variable chitarup 2 775 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