[Python-3000] in-out parameters

Rudy Rudolph rudyrudolph at excite.com
Thu May 4 22:20:06 CEST 2006


 



Guido wrote:>Does Java have them? I know very little Java, but all the other object-oriented languages I use support in-out and out parameters. For example:C++:    void foo(int &param) {param += 5;}    void bar(int &param) {param = 10;}    // C++ does not distinguish between in-out and out parameters.    // call them    int x = 2; foo(x); // x is now 7    int y; bar(y); // y doesn't need to be initialized. It is now 10.    // Unfortunately, the C++ function definition doesn't indicate    // whether the argument needs to be initialized before the call.C#:    void foo(ref int param) {param += 5;} // passed value can be used    void bar(out int param) {param = 10;} // passed value cannot be used    // call them    int x = 2; foo(ref x); // x is now 7    int y; bar(out y); // y is now 10.Ada:    procedure foo(param : in out Integer) is begin param := param + 5; end;    procedure bar(param : out Integer) is begin param := 10; end;    -- call them    Integer x := 2; 
foo(x); -- x is now 7    Integer y; bar(y); -- y is now 10Python:    def foo(paramWrapper):        paramWrapper[0] += 5    # Alternative:    def foo2(param):        return param + 5    def bar(paramWrapper):        paramWrapper[0] = 10    # call them    x = 2    wrapper = [x]    foo(wrapper)    x = wrapper[0] # x is now 7    # Three lines of it just to call foo in such a way that    # it can modify the value of the variable passed in.    # Alternative:    x = 2    x = foo2(x)    # Have to mention x twice just to let foo2 modify its value.    # Also, all the arguments to be modified get mixed in with    # the real function result if there is one.    wrapper = [None]    bar(wrapper)    y = wrapper[0] # y is now 10    # bar is not quite as big of a deal. The new value could    # have been returned as one of (possibly many) results.Proposed Python 3.0:    def foo(&param):        param += 5    def bar(&param):        param = 10    # call them    x = 2    foo(x) # x is now 
7    y = None    bar(y) # y is now 10 I have always considered this the most glaring omission in Python.If it will never happen, fine, it will always be a wart, but in-outand out parameters are common in object-oriented languages and makethe code much more readable. Since we are considering optional type indications on parameters, Ithought this would be a good time to explicitly allow a function tochange the argument value by adding something to the parameter.It could look like foo(&param) or foo(ref param) or foo(inout param)or foo(param:inout) or whatever. I don't really care.The code for both the function definition and the call would be clearer. #Rudy

_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-3000/attachments/20060504/c80ea267/attachment-0001.htm 


More information about the Python-3000 mailing list