-1

I'm having some trouble looping over a list of variables. Specifically, I want to write a function that returns info about the running system. Below is an example of a for loop I tried:

def sysInfo():
    cpu = psutil.cpu_count()
    memory = psutil.virtual_memory()
    disk = psutil.disk_usage('/')

    specs = [cpu, memory, disk]

    for c in specs:
       return c

print sysInfo()

The function runs with no errors but only returns the first item in the list, which leads me to conclude that the loop only executes once. However if I switch the return statement with print (and call the function instead of printing it) then all the items of the list gets printed.

My question then becomes: Why does the loop only give the first item in the list with return, but print gives all of them?

Sifu
  • 153
  • 1
  • 17
  • Take a look at [yield](http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python). Or return a list. Not sure what you're trying to do. – Aran-Fey Jan 26 '15 at 14:44

5 Answers5

0

Once you call return once, you can't "go back" to the function - it finishes running.

Just return the whole list, and print each element individually if necessary.

FuzzyDuck
  • 1,492
  • 12
  • 14
  • I was not aware (new to programming and python). Do you know of any solution that would return all the elements of the list? – Sifu Jan 26 '15 at 14:47
  • If you remove the `for` loop, and simply `return c`, the function `sysInfo` will return a list that contains the cpu, memory and disk variables. You could then put this list into a variable: `x = sysInfo()` and then print the contents collectively: `print x` or individually: `for elt in x: print elt`. – FuzzyDuck Jan 26 '15 at 14:49
0

The return keyword will make your function end immediately. In the first iteration of the loop, when the return statement is reached, your function will end and c will be returned.

What you can do is to return the whole list instead, without using any loop:

return specs
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
0

They are functions and methods. In python we define them in the same way, but they aren't. When you are using return statement, you break loop and return value. If you want return whole list you can just type:

return specs

and then you call variable:

sysInfo()[some_index]

Good idea is change return to dict:

return {"cpu": cpu, "memory":memory, "disk":disk}

and then you can call variable by

sysInfo()["cpu"]
0

When you call return in a function, the function ends its execution and return the given value or None if return is called without a return value.

But why use a for loop at all. you can simply write:

def sysInfo():
    cpu = psutil.cpu_count()
    memory = psutil.virtual_memory()
    disk = psutil.disk_usage('/')

    return cpu, memory, disk

print sysInfo()
Sebastian Stigler
  • 6,819
  • 2
  • 29
  • 31
0

The way you think of this issue is called yield. However in this case just simply return specs for your own good.

John Hua
  • 1,400
  • 9
  • 15