How to pass arguments by reference in Python?

Asked

Viewed 9,397 times

5

When I studied Pascal remember that my teacher explained that to pass an argument by reference it was necessary to put var in front of the variable, in the function/procedure parameters.

Example:

function exemplo(var, a: real):real;

In Python what you need to do to make it happen?

The idea is to have a variable defined at the beginning of the program, change it within the function, and change its value outside the function without using return.

  • If the idea is to change the value of an external variable, use the return and assign again to the variable. Explicit is better than implicit in Python.

  • It is that to get to the function that changes this value you need to go through other 3 or 4. Then you would have to do retreat in all these?

  • Possibly. If you want, you can join the chat we can discuss the code.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

3 answers

5

Python works the same as all languages, everything goes by value. What is different is that some values are accessed directly and others through a reference, ie have objects that are already placed in the internal structure of Python and have objects that are referenced by a value in the internal structure of Python.

Whether this internal structure is accessed by a reference is an implementation detail and makes no difference to you programmer.

So certain values that have semantics by value cannot be passed directly by reference since the language does not provide syntax for this. It offers more complex types that are always by reference. The objects in general are like this, the lists and tuples too, so encapsulate into a more complex object and it’s solved. See:

def value(x):
    x = 1
def ref(x):
    x[0] = 1
x = 0
value(x)
print(x)
x = [0]
ref(x)
print(x[0])

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Is that the idea was to use a variable outside the function inside, change it in the function, and change the value without using Return.

  • That’s what I showed you.

2

If you want to change global variables with objects immutable within the scope of a function, you must use the word global to reference the global object. Example:

def exemplo_imutavel():
    global x
    x = 20


x = 10
exemplo()

print(x)  # 20

In case of variables with objects mutable (lists, set, dictionaries, etc.) the manipulation can be done through parameters in a simple and direct way without the use of the reserved word global. Example:

def exemplo_mutavel(x):
    x[0] = 10


x = [1, 2, 3]
exemplo(x)

print(x)  # [10, 2, 3]
  • 3

    I don’t think so.

  • It’s just that I have a variable that I started with the program. At a certain point, within a function, I try to change it but it only changes within that function, not outside it. I’d like it to change like it’s global.

-6

The python people are not understanding the question. To go by reference in Harbour for example is:

function funcaoX(x)
    x = x + 10
    return nil

y = 5
funcaoX(y)   // Passando sem referência
print(y)     // Retorna 5

funcaoX(@y)  // Passando por referência
print(y)     // Retorna 15

Python is a scripting link, so it is not possible to do this, but there must be a way in Python to pass by reference when the name of the variable within the function is not known and it needs that the variable passed by reference reflects the change of the variable within the function. Remembering that array (list), tuple, dictionary and objects are by nature already passed by reference in most languages.

  • 3

    If you do not know the language or do not master the subject avoid answering with assumptions based on other languages because this only generates confusion. In Python the argument passage model is called passage by assignment which is a hybrid model where primitive types are passed by value and abstract types are passed by reference, with no special syntax or notation to manage this control. Which implies that your example is not appropriate for the language in question.

  • 3

    Another thing, the Stack Overflow is not a forum, we are one Q&A website and its publication in addition to not answering the question asked on the page and still casts a question ...There must be a way in Python to pass by reference.... Use this space only to answer the page question. For more information see [Answer] and [help].

  • 2

    Harbour is also a scripting language. Being scripted or not has anything to do with having a syntax or implicitly passing by reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.