1

I have a scenario where class A is initialized with parameters id,name,email,ticket. Then class B is initialized inside class A's method and the same parameters are required to be passed along and so on and so forth. Can these common attributes be set using a decorator or metaclass during class declaration.

class A:
  def __init__(self, id, name,  email, ticket):
       self.id = id
       self.name=name
       self.email=email
       self.ticket=ticket

  def __somefunc(self):
       # do something
        b = B(self.id, self.name, self.email, self.ticket)
        b.__dosomething()

Similarly...class C and so on.. I am finding it bit inappropriate sending all these repeated parameters to all the subsequent classes.

5
  • What are you trying to achieve? Just less typing? Commented Mar 6, 2019 at 19:45
  • Maybe have A hold onto a dataclass and just pass that around? Commented Mar 6, 2019 at 19:49
  • Is there any further difference between A.__init__ and B.__init__? A class method B.from_A might also be appropriate, to encapsulate the process of creating an instance of B given an instance of A. Commented Mar 6, 2019 at 19:56
  • Can these common attributes be set using ...? set on the instance created in __somefunc only or on A also? Commented Mar 7, 2019 at 1:32
  • Why did you think of using a a decorator or meta class? You really haven't explained your motivation - what problem are you trying to solve? Commented Mar 7, 2019 at 14:28

2 Answers 2

2

If you are just trying to reduce the number of characters typed (appearing in the code), make a dictionary of those values and instantiate the other classes with it.

Class A:
  def __init__(self, id, name,  email, ticket):
       self.id = id
       self.name=name
       self.email=email
       self.ticket=ticket
       self.params = {'id':id, 'name':name,  'email':email, 'ticket':ticket} 

  def __somefunc(self):
       # do something
        b = B(**self.params)
        b.__dosomething()

Can these common attributes be set using a decorator or metaclass during class declaration.? Yes and probably.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use a class decorator to "remember" the parameters as part of the instance, so you would be able to access them using self.args and self.kwargs:

import functools


def remember_params(cls):
    @functools.wraps(cls)
    def instance_creator(*args, **kwargs):
        obj = cls(*args, **kwargs)
        obj.args = args
        obj.kwargs = kwargs
        return obj
    return instance_creator


class B:
    def __init__(*args, **kwargs):
        print(args, kwargs)


@remember_params
class C:
    def __init__(self, id, name, email, ticket):
        self.id = id
        self.name = name
        self.email = email
        self.ticket = ticket

    def something(self):
        b = B(*self.args, **self.kwargs)

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.