0

Given a simple class:

class MyClassA():
    def __init__(self, param_a, param_b):
        
        self.param_a = param_a
        self.param_b = param_b

    def my_method(self, param_c):
        return some_fun(self.param_a, self.param_b)

Now I want to create an instance of this class with just param_b being set. Namely something like:

class MyClassB(MyClassA):
    def __init__(self, param_a):
        
        super().__init__(param_a, something)

Now I want: my_obj_c = MyClassB(some_val).

The question, is there a way to get my_obj_c in one liner without explicitly building MyClassB?

Motivation

The reason I need this is actually this class is used by a different procedure which supports calling the class with a single input.
What I want is to try it (like grid search) which many different values of param_b. This is why setting a default value won't do it.

13
  • 1
    So param_a is optional? Mar 18 at 22:37
  • @Unmitigated, no, I just missed it. Fixing it. Thanks for the heads up. Mar 18 at 22:42
  • What do you mean by "without explicitly building MyClassB"? Mar 18 at 22:45
  • It's not clear what you mean by get my_obj_c. In your code my_obj_c is an instance of MyClassB -- that's it's type. Is that what you want, or are you really trying to make a MyClassA without specifying param_b?
    – Mark
    Mar 18 at 22:46
  • You could make the default arguments of A optional. This would however maintain the type of MyClassA and not MyClassB Mar 18 at 22:55

2 Answers 2

3

You could use functools.partial to make a partial of the class constructor with the second argument pre-set.

import functools 

class MyClassA():
    def __init__(self, param_a, param_b):        
        self.param_a = param_a
        self.param_b = param_b

    def my_method(self):
        return f"{self.param_a} {self.param_b}"

Paramterized = functools.partial(MyClassA, param_b="world")

my_instance = Paramterized("hello")
# <__main__.MyClassA at 0x112816e00> 
my_instance.my_method()
#'hello world'

my_instance2 = Paramterized("goodbye cruel")
my_instance2.my_method()
# 'goodbye cruel world'
2
  • This seems to be like I wanted. Is there a way to make it in a single step? Namely create the my_instance... in a single line? Mar 19 at 9:06
  • 1
    I think this is where everyone is confused about what you are trying to do. partial(MyClassA, param_b="world")("hello") would work, but is clearly worse than just writing MyClassA("hello", "world"). Unless you plan on using Parameterized somewhere other than where you defined it, there's no need to define it in the first place.
    – chepner
    Mar 19 at 14:38
0

What do you think about

class MyClassA():
    def __init__(self, param_a, param_b=SomeValue):
...

this way you can create instance like this:

obj1 = MyClassA(param_a, param_b)
obj2 = MyClassA(param_a)

In the second instance the param_b will be someValue

1
  • The issue is I need to have this parameterized with many different values. So a default value won't solve it. Mar 18 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.