0

When giving a string-parameter to a function which i want to use as an argument inside the function for another function:

def function(classifier, classifier_argument, list_of_parameters):
    classifier(classifier_argument=list_of_parameter[0])

classifier = someClassifier()
classifier_argument = 'someArgument'
list_of_parameters = [0,1,2,3,4,5]

func(classifier,classifier_argument,list_of_parameters)

It gives me this error:

TypeError: 'classifier_argument' is an invalid keyword argument for this function

So I know I can't pass it as a simple string, so my question is:

How can i pass it?

3 Answers 3

2

In my guess, you are trying to achieve your classifier creation class to be initiated with different attributes as follows ? You can create class attributes dynamically on you __init__ method and use **kwargs to send the required data.

class someClassifier(object):

    def __init__(self, **kwargs):
        for k, v in kwargs.items():
            setattr(self, k, v)
            print('setting classifier attribute', k, v)


def function(classifier, **kwargs):
    classifier(**kwargs)


classifier = someClassifier

list_of_parameters = [0, 1, 2, 3, 4, 5]
classifier_argument = {
    'someArgument': list_of_parameters
}


function(classifier, **classifier_argument)
Sign up to request clarification or add additional context in comments.

2 Comments

actually its a class from sklearn, so you suggest to manipulate the init in this library or write this someClassifier() class by myself somehow? Actually its my goal to go directly in the classifier, because it should be dynamic for other classifiers as well.
If you using sklearn then it will not be a problem to instantiate any of the classifiers. But, you need to make sure the parameters match in your call. So, the above approach will work. Just you need to send right parameters to your function. Like I did above using dict.
0
def function(classifier, classifier_argument, list_of_parameters):
    args = {classifier_argument:list_of_parameters}
    classifier(**args)

classifier = someClassifier # Note - you need to reference the function itself, not call it!
classifier_argument = 'someArgument'
list_of_parameters = [0,1,2,3,4,5]

function(classifier,classifier_argument,list_of_parameters)

A couple of points here: you have to unpack the arguments list, and you need to pass in the classsifier function without calling it.

4 Comments

This produces me another error: TypeError: 'someClassifier' object is not callable. Note: It doesn't make a difference when i reference my classifier before.
Can you point out what your error is? I manage to run this code fine (though obviously I've defined my own someClassifier function, which has an argument called "someArgument".
I might be misunderstanding your intent. Perhaps you should just do classifier = someClassifier() after all?
Ok i got the problem resolved, it was the reference you pointed out. I should edit it. My problem was that i referenced the function i wanted to use before function call. It worked when I referenced it. Note: I used this classifier for another function inside the function, so the solution from John Go-Soco perfectly fits my stated problem (just not the one I was actually working on). Thanks a lot.
0

You could use ** when call function:

def function(classifier, classifier_argument, list_of_parameters):
    paras = {classifier_argument: list_of_parameter[0]}
    classifier(**paras)

classifier = someClassifier()
classifier_argument = 'someArgument'
list_of_parameters = [0,1,2,3,4,5]

func(classifier,classifier_argument,list_of_parameters)

A more simple workable example for your quick test:

def fun1(b):
    print(b)

def fun(a):
    paras = {a: 2}
    fun1(**paras)

fun('b')

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.