4 Replies - 852 Views - Last Post: 15 November 2011 - 09:33 AM Rate Topic: -----

#1 cryptoxic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 22-January 11

syntax for using variables in a function from another function

Posted 14 November 2011 - 10:16 PM

Hello dreamincoders,
I am at a point in my code, where I need to get a few variables from one function and use it in the other. How can I access/use them? For example:

def f1(M):
    #Creating lists
    a=[]
    b =[]
    c=[]

    . 
    .
    .

    for i in range(0, len(M)):
        a.append(i)
    .
    .
    .
def f2():
    #I need those lists here!


Is This A Good Question/Topic? 0
  • +

Replies To: syntax for using variables in a function from another function

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: syntax for using variables in a function from another function

Posted 15 November 2011 - 04:21 AM

If you want to use the lists created in f1 from the call-site you should return them. So something like this:

def f1(M):
    # Create a, b and c
    return a,b,c

def f2():
    a,b,c = f1(whatever)
    # Do something with a, b and c



PS: I don't know whether that was just an example or not, but a=[] for i in range(0, len(M)): a.append(i) is just a needlessly verbose way to say a = range(0, len(M)).

This post has been edited by sepp2k: 15 November 2011 - 04:23 AM

Was This Post Helpful? 1
  • +
  • -

#3 cryptoxic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 22-January 11

Re: syntax for using variables in a function from another function

Posted 15 November 2011 - 08:54 AM

View Postsepp2k, on 15 November 2011 - 04:21 AM, said:

If you want to use the lists created in f1 from the call-site you should return them. So something like this:

def f1(M):
    # Create a, b and c
    return a,b,c

def f2():
    a,b,c = f1(whatever)
    # Do something with a, b and c



PS: I don't know whether that was just an example or not, but a=[] for i in range(0, len(M)): a.append(i) is just a needlessly verbose way to say a = range(0, len(M)).

Thanks a lot for providing this solution. It worked like a charm. The deal with my verbose ways of filling lists comes from my C/C++ programming habits, as it is my native programming language. Hence the miss with returning multiple variables, which is not directly supported in C. The code is more comprehensive to me that way. But thanks for showing a more compact alternative. I might as well practice writing python the way it is supposed to be written.
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is online

  • Dreaming Coder
  • member icon


Reputation: 6996
  • View blog
  • Posts: 14,635
  • Joined: 16-October 07

Re: syntax for using variables in a function from another function

Posted 15 November 2011 - 09:21 AM

View Postsepp2k, on 15 November 2011 - 07:21 AM, said:

a = range(0, len(M)).


Just a quick FYI on that; will not work in Python 3.x. However, a list comprehension works in both 3 and 2. Best to pick up the habit early.

e.g.
>>> M = 10
>>> a = range(M)
>>> a
range(0, 10)
>>> a = [i for i in range(M)]
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 


Was This Post Helpful? 2
  • +
  • -

#5 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2517
  • View blog
  • Posts: 4,001
  • Joined: 21-June 11

Re: syntax for using variables in a function from another function

Posted 15 November 2011 - 09:33 AM

View Postbaavgai, on 15 November 2011 - 06:21 PM, said:

Just a quick FYI on that; will not work in Python 3.x.


Well, it will make a a generator instead of a list in python 3.x - whether that's bad depends on the use case, I suppose. You can convert it to a list using list if you need to (which also works both in 2.x and 3.x), which I personally find more straight forward than using a list comprehension in this case.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1