0

I have a class like

class myClass():
    def filter(self, inplace = False, **kwargs):
        for key, value in kwargs.items():
            if isinstance(value, str):
                value = "'%s'" % value
            query_str += " & %s == %s" % (str(key), str(value))

myObject.filter(param1 = value1, inplace = False)

When I run it, I get an error message:

pandas.computation.ops.UndefinedVariableError: name 'param1' is not defined

I don't really understand it, because the reason you use **kwargs is to be more flexible when it comes to parameters, right?

Sam Hartman
  • 6,210
  • 3
  • 23
  • 40
JungleDiff
  • 3,221
  • 10
  • 33
  • 57
  • 1
    What happens when you run this: myObject.filter(inplace = False, param1 = value1) – Boudewijn Aasman Jun 16 '17 at 15:40
  • Possible duplicate of https://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-parameters – Bubble Bubble Bubble Gut Jun 16 '17 at 15:41
  • 1
    Possible duplicate of [What does \*\* (double star) and \* (star) do for parameters?](https://stackoverflow.com/questions/36901/what-does-double-star-and-star-do-for-parameters) – Bubble Bubble Bubble Gut Jun 16 '17 at 15:41
  • I don't think this is a dup of * and ** for parameters. The OP's understanding of kwargs seems correct for python, the issue is the interaction with pandas. – Sam Hartman Jun 16 '17 at 15:50
  • I get this error message: pandas.computation.ops.UndefinedVariableError: name 'param1' is not defined. Sam, you maybe right. The issue may be related to pandas. What am I supposed to do then now? – JungleDiff Jun 16 '17 at 15:52
  • Unfortunately, you need to show more code or a more realistic example. I appreciate that you're trying to give us a [mcve], and that's good, but you've trimmed things too much. No where in your code do you show anything to do with pandas, but your error is a pandas error. I expect that if you look at that code in a python program without any pandas involved at all, it will work as you expect. – Sam Hartman Jun 16 '17 at 15:53

1 Answers1

-1

inplace is a positional argument and must come before keyword arguments. Try

myObject.filter(False, param1 = value1)
tso
  • 4,732
  • 2
  • 22
  • 32
figbeam
  • 7,001
  • 2
  • 12
  • 18
  • It's totally not a positional argument. In a user-defined function, any argument named in the parameter list can be passed by keyword, and if so passed, order does not matter. – Sam Hartman Jun 17 '17 at 19:22