1

My title might be a bit off but I didn't really know how to formulate myself.

Say I have a function that takes a data structure and maybe some other arguments and returns a value from that data structure. How do I reassign that return value?

Here's a simple example of what I want to do:

Let's say I have a function foo,

def foo(x, n):
    return x[n]

and a list l,

l = [1, 2, 3, 4, 5]

what I want to do is something like this:

foo(l, 1) = 0 #this results in an error

Which I want to result in this:

l = [1, 0, 3, 4, 5]

This example may be trivial with another approach but let's imagine my function for retrieving an item from the list is more complex.

Edit: Maybe my description was too unclear, basically what I want is an assignment operator that works like setf in lisp. I want my function to return some sort of pointer to a value in a data structure. I then want to change the value that the pointer is pointing at. What I want to use it for i basically something like this: (that I don't understand why I didn't just begin with as an example)

def foo(l, indexes):
    if indexes == []:
        return l
    else:
        return foo(l[indexes[0]], indexes[1:])
l = [[1, 2], 3]
foo(l, [0, 1]) = 0 #Again this is what is not working and I understand why it's not working, I just want something that would do what you think this might do.

Which I want to result in:

l = [[1, 0], 3]
Fernet
  • 173
  • 1
  • 10
  • When the function returns the value 2, the value is no longer `l[1]`, but is just a number 2. It is not a valid variable to assign to. What you need to do is pass the number `0` into the function, and assign it to `l[1]` inside the function. – SiddharthaRT Nov 19 '13 at 18:18
  • @ndpu, no, that's not exactly what OP is asking for. `foo` returns a value, you can't use it as an index. – SiddharthaRT Nov 19 '13 at 18:20
  • Since `foo` returns an `int` (in this case), you **CANNOT** assign any other `int` (0) to it! – shad0w_wa1k3r Nov 19 '13 at 18:28
  • Python does not have that kind of "pointer". You realize the problem already from the phrasing of the question: the function returns **a value**, which doesn't have any of the data structure's context. "I want my function to return some sort of pointer to a value in a data structure" If that data structure is a simple `list`, then the "pointer" you want is... an integer which is an index into that list. But usually it would be better to let the function do the assignment. – Karl Knechtel Aug 10 '22 at 00:13

3 Answers3

1

Is this what you intend?

def foo(thelist, n):
     thelist.append(n)
     return thelist

OR are you looking to insert?

def foo(thelist, i, n):
     thelist.insert(i , n)
     return thelist

@OP after reading you're edited post; check out link and link there's an example there that may be of use.

According to the stackoverflow post; the answer is no.

Community
  • 1
  • 1
crownedzero
  • 496
  • 1
  • 7
  • 18
0

I still dont understand what you are trying to achieve, so please elaborate on that. Now it looks like what you need is

def foo(l, n, number):
    ...logic...
    l[n] = number
    return l

Edit: After reading your edit, no. the function returns the value, not a reference to the item in the list.

SiddharthaRT
  • 2,217
  • 4
  • 20
  • 28
0

What you ask for is not possible(The reason is you cannot assign 2 to 0). Instead of using function if you use macro, then you can achieve it. python doesn't have macros as C. But once have a look at macropy to write macros in python.

Sagar Shah
  • 123
  • 1
  • 5