问题
I have Python client calls a Thrift service with some optional parameters like this:
bool postTweet(1: required Tweet tweet, 2: i32 x = 100);
If I tried to call this service from Python client without passing the optional parameter x, I get an exception:
TypeError: postTweet() takes exactly 2 arguments (1 given)
Any clues why I get this exception however it is optional parameter with a default value?
回答1:
It's not possible to define a thrift function with default value (well at least from my understanding after reading the thrift whitepaper)
What you can do, is to define a special parameter struct that let you omit some of the fields.
Example thrift code:
struct PostTweetParameter {
1: required Tweet tweet;
2: optional i32 x;
}
bool postTweet(1: PostTweetParameter param);
Then you can construct the PostTweetParameter
with field x
omitted.
回答2:
The x argument must be defined sepetate from the other parameters. Otherwise, it can mess other things up. To create optional parameters, your function must look similar to this: def function(x = 0): With this, you may choose what value x is using arguments, but if you call the function by saying function() X is automatically assigned to 0. Remember, x must be set up in the parameters by itself, not mixed invwith other parameters.
来源:https://stackoverflow.com/questions/14878004/default-arguments-with-default-values-in-thrift-python-client