0

I have a function get_arcs():

for i in rng:
    if not lines[i].startswith('[') and 'echo $ORACLE_SID' in lines[i].strip():
            sid = lines[i + 2]
            yield sid
    if lines[i].strip().find('SELECT') == 0:
        res = lines[i:]
        for i in res:
            print >> f, i.strip()
            yield i.strip()

which supposed to:

  1. assign a certain value to variable sid
  2. write different content to f-file
  3. have the generator of i.strip() as the output which will be passed the to another function and the sum of certain objects will be calculated

The problem is when the get_arcs() is called:

for line in get_arcs():
    if len(line.split()) == 13:
        lst.append(int(line.split()[-5]))
print sum(lst), 'Bytes'

It does return the sum(lst) as expected, but how can I retrieve my sid value, as I cannot call it anywhere outside of the function.

1 Answer 1

1

So, you need this function to return both i.strip() and sid? I always find it a code smell when you try to return two values from a single function. But if it really is necessary in this case, I would return a tuple.

yield i.strip(), sid

Later:

for (line, sid) in get_arcs():
    # both line and sid are defined here
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, this worked just fine. Thanks a lot! P.S. yes I do need 1 func to return two values, since there is a paramiko ssh connection gets established as well.
Of course, there's an exception to every rule. It's just always worth double checking in these cases. ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.