Sending string var with apply sends all chars as params!???
walter at livinglogic.de
Tue Jun 25 11:45:49 EDT 2002
> Hi all,
> I am having problems with the apply function in Python. When applying a
> string variable to a method, the string var gets chopped up into all it
> chars and each char gets sent as a parameter! The result is getting a
> TypeError because I am sending too much parameters to the function (more
> detail below if you still don't understand)
> Detailed explanation:
> When running my program I get the following:
> My print line debugging string output:
> Formatting value '20020625140609' of type <type 'string' with formatter
> <function TimeFormatter at 00D613F4>
> as you can see, I am sending the string '20020625140609' to the function
> The code that does this is:
> print "UCP: Formatting value '%s' of type '%s' with formatter '%s'"
> %(value, type(value), formatter)
> value = apply(formatter,(str(value)))
This should be
value = apply(formatter,(str(value),))
But you have only one argument, so this can be simply written
value = formatter(str(value))
> in this case, formatter is the function TimeFormatter (as defined in a
> mapping table)
> When this code executes I get a TypeError:
> TypeError: TimeFormatter() takes exactly 1 argument (14 given).
> Why is the string (which is 14 chars long) broken up into 14 values when
> passed using apply!?
Because apply expects a sequence of arguments as the second parameter.
> The weird thing is this code has been working for MONTHS and have only
> recently stopped working after some refactoring in a completely
> different part of code!
> The problem goes away when I don't use apply:
> value = formatter(value)
More information about the Python-list