ProtoRPC method overloading

27 views
Skip to first unread message

perrier

unread,
Sep 18, 2012, 8:22:43 AM9/18/12
to google-prot...@googlegroups.com
I'm new to python and protorpc, just trying to find the right platform for a new project we have started. I'd like to define some methods with the same name but different parameters.
Is this possible?

Rafe Kaplan

unread,
Sep 18, 2012, 3:21:25 PM9/18/12
to google-prot...@googlegroups.com
Please give me some examples of what you mean. Are you meaning to
override service method names or are you trying to do Python/C++ style
method overloading?
--
- Rafe Kaplan

perrier

unread,
Sep 18, 2012, 3:38:00 PM9/18/12
to google-prot...@googlegroups.com
I'd like to do Python/C++ style method overloading to publish a service that has for example two Add methods with different signatures. Running on the top of AppEngine. 

Rafe Kaplan

unread,
Sep 18, 2012, 4:18:56 PM9/18/12
to google-prot...@googlegroups.com
Sorry, Peter! There are actually two ways in which what you're
trying to do won't work :/

First, Python does not support the kind of method overloading you
are thinking about. A method on an object instance is actually itself
a very simply object which always calls the same function. Don't be
disheartened. In practice, Python developers have found they don't
need to use overloaded functions as the parameter system in Python has
it's own flexible advantages. For example, in Java you might do:

int myFunction(String value1) {
...
}

int myFunction(String value1, int value2) {
...
}

In Python, you can use parameter defaults to achieve that kind of
functionality pretty easily:

def my_function(value1, value=20):
...

You still need to handle multiple type parameters inside the
function. There is not that kind of type safety.


The second issues is with Protobuffers, which is the underlying
protocol the ProtoRPC implements. It only supports single message
methods. There are some techniques you can use to make them more
overridable. One very simple way, that is similar to Python, is that
it supports default message values (although the current
implementation does not handle those so gracefully). Another way is
like so:

class MyRequest(Message):
param1 = messages.MessageField(Param1Type, 1)
param2 = messages.MessageField(Param2Type, 2)

The caller will set one parameter OR the other but not both.

I'm not aware of any RPC systems other than Java RMI that supports
method overloading.
--
- Rafe Kaplan

perrier

unread,
Sep 19, 2012, 2:36:51 AM9/19/12
to google-prot...@googlegroups.com
Thank you for your quick answers. Sorry to hear that, but at least now I know how it can't be planned. :) 
I'm coming from the .NET WCF world where you can simply use the OperationContract attribute to give the method a different name.

Do you know when will ProtoRPC be stable enough to be used in an enterprise application?
Reply all
Reply to author
Forward
0 new messages