Python Forum
function that run once for all objects
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function that run once for all objects
#9
I think having the __init__ method check a class variable should work great for this.
class Thing:
    parameters = None  # Stuff you want to read once and use many times

    @classmethod
    def init_parameters(cls):
        '''Load parameters from a file. All instances will share these parameters'''
        # Open file an read parameters
        cls.parameters = whatever()

    def __init__(self):
        if self.parameters is None:
            self.init_parameters()  # Initialize these class variables from a file.  Happens once.
        #Can use the parameters now
Sounds like all instances will share these parameters, so it makes sense to use a class variable to reference them.

If this is not applicable to your problem, why not? Can you explain please.
mr_byte31 likes this post
Reply


Messages In This Thread
RE: function that run once for all objects - by deanhystad - Oct-15-2021, 03:50 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  AWS Boto3 - objects function PythonDialer 0 217 May-19-2024, 11:50 AM
Last Post: PythonDialer

Forum Jump:

User Panel Messages

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