Pass to/ Receive Same Variable From Function

A forum for general discussion of the Python programming language.

Pass to/ Receive Same Variable From Function

Postby forum_dweller » Mon Jul 07, 2014 8:21 pm

I want to pass several variables to a function to be changed, but then I want the function to return the same variables so I can continue using them (The function is changing the value(s), but I want the same variable name(s)):

variable1, variable2, variable3 = function1(variable1, variable2, variable3)

Does this line mean that I am initializing new variables, or that I am simply redefining a new/existing variable? I think I have had issues with this before that is why I am asking. Is simply creating new variables the easiest solution here?

variable4, variable5, variable6 = function1(variable1, variable2, variable3)

Any clarification would be appreciated.
forum_dweller
 
Posts: 9
Joined: Sun Jun 29, 2014 7:18 pm

Re: Pass to/ Receive Same Variable From Function

Postby stranac » Mon Jul 07, 2014 10:55 pm

Not sure what exactly you're asking here.
Is there an actual problem you're trying to solve?
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1246
Joined: Thu Feb 07, 2013 3:42 pm

Re: Pass to/ Receive Same Variable From Function

Postby micseydel » Mon Jul 07, 2014 10:59 pm

You can use *args to have a variable number of arguments to the function, and then just have it return that many values in a tuple. If this doesn't help, then I'm in the same boat as stranac about needing more information.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1505
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Pass to/ Receive Same Variable From Function

Postby Yoriz » Mon Jul 07, 2014 11:50 pm

It depends on weither the arguments you pass to the function are mutable(can be altered) or immutable(can not be altered) and what with you do with them inside the function.

If they are immutable and you return as variable4, variable5, variable6 these will point at the new returned objects, variable1, variable2, variable3, will still point to their orignal objects.

If they are mutable (say lists and you append to them) and you return as variable4, variable5, variable6, these will point at the same objects as variable1, variable2, variable3. variable1, variable2, variable3, will point to their orignal modified objects.

If they are immutable and you return as variable1, variable2, variable3, these will point at the new returned objects, variable1, variable2, variable3, will no longer point to their orignal objects.

If they are mutable (say lists and you append to them) and you return as variable1, variable2, variable3, these variable have just been re-pointed at the same objects.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1170
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 3 guests