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.

GiulioDP
  • 1
  • 1
  • 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. – aufziehvogel Dec 16 '12 at 17:40
  • 3
    @Aufziehvogel: 'cathing them' is called tuple assignment. And using that is *exactly* how it should be done in Python. – Martijn Pieters Dec 16 '12 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? – GiulioDP Dec 16 '12 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**. – NlightNFotis Dec 16 '12 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. – NlightNFotis Dec 16 '12 at 17:56
  • @NlightNFotis Actually, in CPython, the object `5` will never be garbage collected since there is a cache for small integers(but, if it was an other object than your statement would be 100% correct). – Bakuriu Dec 16 '12 at 17:59
  • @Bakuriu Yeah, I know. I remembered this when I started my example, and forgot about it when I finished it. LOL. – NlightNFotis Dec 16 '12 at 18:01

2 Answers2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
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)

Community
  • 1
  • 1
NlightNFotis
  • 9,559
  • 5
  • 43
  • 66