0

I'm sorry to repeat the same thing all over again but I've already searched but I still can't solve my problem...

If I want to change a single "int" value, do I HAVE to create a list? It's a mess. It's quite more comfortable in C... (like void function(int a, b, c, *d, *e, *f))

This is what I'm trying to do (I HAVE to change "d", "e", "f"):

d = 4
e = 9
f = 11
def function(a,b,c,d,e,f)
   a = something
   b = something
   c = 1
   d = 2
   e = 3
   f = 4

Having said this, do I have to create a dictionary or a list to handle simple numbers? I'm still a newbie but I'd like to know what's the best solution for this problem that is scrambling my mind lately.

Thank you guys.

7
  • You could return them as a tuple and catch them d,e,f = function(a,b,c,d,e,f), but I guess there are better ways. Commented Dec 16, 2012 at 17:40
  • 3
    @Aufziehvogel: 'cathing them' is called tuple assignment. And using that is exactly how it should be done in Python. Commented Dec 16, 2012 at 17:43
  • Thank you guys. This solved the problem! But, since I'm curious... what is there behind this? I mean. When I create "a" (which is an object right?) there's a pointer pointing to "a". If I give to "a" another value, then ANOTHER object is created (and not overwritten like in C). So, when I return the tuple what is happening? Commented Dec 16, 2012 at 17:51
  • @GiulioDePasquale You return a tuple object that hosts several other objects. These are new objects and get to be referenced in the place of the old objects when you do the sequence unpacking. Commented Dec 16, 2012 at 17:54
  • @GiulioDePasquale It's essentialy like doing a = 5 and then a = 7. This assigns a different object to the reference, and due to the nature of the python garbage collection (reference counting) the int object 5 gets garbage collected some time after the second statement. Commented Dec 16, 2012 at 17:56

2 Answers 2

5

Yes. Because python can simply return a tuple, you use that to change multiple values:

d = 4
e = 9
f = 11
def function(a,b,c)
   a = something
   b = something
   c = 1
   d = 2
   e = 3
   f = 4
   return (d, e, f)

d, e, f = function(d, e, f)

Since passing by reference is mainly used (in C at least) to work around the fact you cannot return more than one value from a function, you don't need passing by reference in Python.

Sign up to request clarification or add additional context in comments.

1 Comment

So I can retrieve more than a value? That's great. Thank you very much.
2

There is a question (and answer) here on S.O that explains what you need to know regarding passing objects as parameters.

Regarding your code, you could always return those values in a tuple and assign them to the references of thos objects in the global namespace, likewise: return (d, e, f). Now to assign them to references in your objects, you could use sequence unpacking, eg d, e, f = function(params)

1 Comment

@GiulioDePasquale Glad I could be of help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.