Python method not executed
<p>Total noob here. I'm trying to create a python object and execute methods in an instance therein and it seems the code block I want to execute just won't run. The code block in question is run_job which when called just seems to do nothing. What am I doing wrong?</p>

<pre><code>import datetime
import uuid
import paramiko


class scan_job(object):

    def __init__(self, protocol, target, user_name, password, command):
        self.guid = uuid.uuid1()
        self.start_time = datetime.datetime.now()
        self.target = target
        self.command = command
        self.user_name = user_name
        self.password = password
        self.protocol = protocol
        self.result = ""

    def run_job(self):
        if self.protocol == 'SSH':
            ssh = paramiko.SSHClient()
            ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
            try:
                print "creating connection"
                ssh.connect(self.target, self.user_name, self.password)
                print "connected"
                stdin, stdout, stderr = ssh.exec_command(self.command)
                for line in stdout:
                    print '... ' + line.strip('\n')
                    self.result += line.strip('\n')
                yield ssh
            finally:
                print "closing connection"
                ssh.close()
                print "closed"

        else:
            print "Unknown protocol"

    def show_command(self):
        print self.command


test = scan_job('SSH', '192.168.14.10', 'myuser', 'mypassword', 'uname -n')

test.show_command()

test.run_job()
</code></pre>

<blockquote>
  <p>Yield is a keyword that is used like return, except the function will
  return a generator.</p>
</blockquote>

<p>To read more about generators:<br>
1) <a href="http://stackoverflow.com/questions/1756096/understanding-generators-in-python">Understanding Generators in Python</a><br>
2) <a href="http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do">What does the &quot;yield&quot; keyword do?</a><br>
3) <a href="https://www.sitepoint.com/quick-tip-understanding-the-yield-keyword-in-python/" rel="nofollow">https://www.sitepoint.com/quick-tip-understanding-the-yield-keyword-in-python/</a></p>

<p>All you need to do is, change:</p>

<pre><code>yield ssh
</code></pre>

<p>To:</p>

<pre><code>return ssh
</code></pre>

<p>So that, run_job will execute like a normal function, until it reaches its end, exception or return statement. However, if you want to run it without changing the yield statement. Here is how you can do it:</p>

<pre><code>x = test.run_job()
x.next()
</code></pre>

python python-2.7