Send by ref/by ptr in python?
Question: i need help-i try to send value to method like in c++ by ref/by ptr how can i
do it?

to exmple:

    
    
     def test(x):
        x=3    
    
     x=2
     test(x)
     print(x)
    

In this case x a local variable in test method and will not change the
"original" X so how can i change the "original" X? thanks


Answer: In some ways, all calls in Python are called with references. In fact, all
variables are references in a sense. But some types, like `int` from your
example, cannot be changed.

In the case of, say, a `list`, the functionality you're looking for is
trivial:

    
    
    def change_it(some_list):
        some_list.append("world")
    
    foo = ["hello"]
    change_it(foo)
    print(foo)  # prints ['hello', 'world']
    

Note, however, that reassigning the _parameter_ variable `some_list` does not
change the value in the calling context.

If you're asking this question, though, you're probably looking to do
something like set two or three variables using one function. In that case,
you're looking for something like this:

    
    
    def foo_bar(x, y, z):
        return 2*x, 3*y, 4*z
    
    x = 3
    y = 4
    z = 5
    x, y, z = foo_bar(x, y, z)
    print(y)  # prints 12
    

Of course, you can do anything in Python, but that doesn't mean you should. In
the fashion of the TV show Mythbusters, here's something that does what you're
looking for

    
    
    import inspect
    
    def foo(bar):
        frame = inspect.currentframe()
        outer = inspect.getouterframes(frame)[1][0]
        outer.f_locals[bar] = 2 * outer.f_locals[bar]
    
    a = 15
    foo("a")
    print(a) # prints 30
    

or even worse:

    
    
    import inspect
    import re
    
    def foo(bar):
        # get the current call stack
        my_stack = inspect.stack()
        # get the outer frame object off of the stack
        outer = my_stack[1][0]
        # get the calling line of code; see the inspect module documentation
        #   only works if the call is not split across multiple lines of code
        calling_line = my_stack[1][4][0]
        # get this function's name
        my_name = my_stack[0][3]
        # do a regular expression search for the function call in traditional form
        #   and extract the name of the first parameter
        m = re.search(my_name + "\s*\(\s*(\w+)\s*\)", calling_line)
        if m:
            # finally, set the variable in the outer context
            outer.f_locals[m.group(1)] = 2 * outer.f_locals[m.group(1)]
        else:
            raise TypeError("Non-traditional function call.  Why don't you just"
                            " give up on pass-by-reference already?")
    
    # now this works like you would expect
    a = 15
    foo(a)
    print(a)
    
    # but then this doesn't work:
    baz = foo_bar
    baz(a)  #  raises TypeError
    
    # and this *really*, disastrously doesn't work
    a, b = 15, 20
    foo_bar, baz = str, foo_bar
    baz(b) and foo_bar(a)
    print(a, b)  # prints 30, 20
    

Please, _please_ , _**please**_ , don't do this. I only put it in here to
inspire the reader to look into some of the more obscure parts of Python.

