1

Let's imagine i have class

class NewSettingsAdded:  # type: NewSettingsAdded
    def __init__(self, system: str, org_id: ):

        self.system = system
        self.org_id = org_id

    def find_head_organization(self):
        "do some prepare"

    def process(self):
    " process"

I would like to after instance initialization automatiucally call only find_head_organization for self. I tried to do something like

class OrganisationCaller(type):
    def __call__(cls, *args, **kwargs):

        obj = type.__call__(cls, *args, **kwargs)
        obj.new_init()
        return obj


class NewSettingsAdded:  # type: NewSettingsAdded
    def __init__(self, system: str, org_id: int,):

    __metaclass__ = OrganisationCaller

    self.system = system
    self.org_id = org_id

    def find_head_organization(self):
        "do some prepare"

    def process(self):
        " process"

But in this approach, it calls all methods after initialisation

3
  • 4
    why don't you just call find_head_organization at the end of __init__ in NewSettingsAdded? Commented Jan 22, 2020 at 8:39
  • Yes, i can do this, i am just trying to find another way, maybe from decorator or something Commented Jan 22, 2020 at 8:45
  • 3
    But why? You can easily create a decorator for __init__ that calls some other function, but again, why? Commented Jan 22, 2020 at 8:46

1 Answer 1

2

If your goal is for the object to be "automatically executed upon class instantiation", just put self.run() in the init:

def __init__(self):
    self.bar = 1
    self.run()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.