How do I execute a function in a certain way after importing it in another python file?
<p>This is a function that returns a list of length of random numbers from 0 to 9.</p>

<pre><code>import random

def makeList():
    y=[]
    n=int(raw_input("Enter number"))

    for x in range(0,n):
        x=random.randint(0,9)
        y.append(x)
    print y

def main():
    makeList()
if __name__ == '__main__':
    main()
</code></pre>

<p>Now i am creating another python file called main.py and importing this function
this is the code i wrote in main.py . The name of the file is myFunc.</p>

<pre><code>from myFuncs import makeList
makeList()
</code></pre>

<p>Now, 
    I want to create a list of ( say 10 random numbers.) what should i write inside the main.py file to achieve this ?</p>

<p>Changing <code>print y</code> to <code>return y</code> in the first file. (and <code>print(makeList())</code> under <code>main()</code>) </p>

<p>In the new file, <code>new_list = makeList()</code></p>

<p>then new_list is that generated list.</p>

<p>Edit: More pythonic version</p>

<p><strong>File 1</strong></p>

<pre><code>import random

def make_list(length=10):
    return [random.randint(0,9) for _ in range(length)]

def main():
    amount = int(raw_input("Enter number"))
    print(make_list(amount))

if __name__ == '__main__':
    main()
</code></pre>

<p><strong>File 2</strong></p>

<pre><code>from myFuncs import make_list

new_list = make_list()
print(new_list) # will be default 10
</code></pre>

<p><strong>--</strong></p>

<p>If you want to keep the input in that same function it is possible, here is one way: </p>

<pre><code>def make_list(length=0):
    if not length:
        length= int(raw_input("Enter number"))    
    return [random.randint(0,9) for _ in range(length)]

def main():
    print(make_list())
</code></pre>

<p>Then specify <code>make_list(10)</code> in the second file</p>

python python-2.7