4

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?

Montaro
  • 9,240
  • 6
  • 29
  • 30

2 Answers2

6

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.

yegle
  • 5,795
  • 6
  • 39
  • 61
1

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.

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • I didn't understand this, can you explain with an example please? – Ahmed Farghal Feb 16 '13 at 15:30
  • I'm afraid I did not understand you, can you please explain with code example? – Montaro Feb 16 '13 at 15:30
  • when setting parameters, the individual parameters are best set by itself, like `def function(x = 0, y = 1)`. When you tried combining the parameters with `i32', it messed the function up. If you want to associate values like this, you should do it withing the function, so that the parameters can be set. This would work if you did not assign a value, like `bool postTweet(1: required Tweet tweet, 2: i32 x )` however, by assigning a value, it messes up the parameters. You should send `x` in as its own parameter, and then associate it with `i32`, depending on what the function does. Otherwise, – erdekhayser Feb 16 '13 at 16:13
  • 1
    you can do it by saying `bool postTweet(1: required Tweet tweet, 2: i32 x)` and check if it has a NoneType value, and then assign it to the default. `if x == None: x = 0` – erdekhayser Feb 16 '13 at 16:14