Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Depending on how flexible you want to have your Python script, I would structure the script like so:</p> <pre><code>def get_args(): # logic for parsing arguments here # return e.g. a dictionary def your_method(arg1=None,arg2=...): # further logic if __name__ == "__main__": args = get_args() your_method(**args) </code></pre> <p>There are various modules to parse command line arguments. Have a look at <a href="http://docs.python.org/dev/library/argparse.html" rel="nofollow noreferrer"><code>argparse</code></a> (easier) and <a href="http://docs.python.org/library/optparse.html" rel="nofollow noreferrer"><code>optparse</code></a>.</p> <p>If you just need a simple way to access the command line arguments, you can go with <a href="http://docs.python.org/library/sys.html#sys.argv" rel="nofollow noreferrer"><code>sys.argv</code></a>.</p> <p>With this separation you are also able to import your function into other code.</p> <p>Example:</p> <pre><code>import sys def get_args(): word = sys.argv[1] if (len(sys.argv) &gt; 1) else '' return {"word": word} def your_method(word=''): for i in range(len(word)): print word[i:] + word[:i], (word[i:] + word[:i])[::-1] if __name__ == "__main__": args = get_args() your_method(**args) </code></pre> <p>and run it with</p> <pre><code>python yourscript.py someword </code></pre> <hr> <p><strong>See also:</strong></p> <ul> <li><a href="https://stackoverflow.com/questions/567879/how-can-i-process-command-line-arguments-in-python">How can I process command line arguments in Python?</a></li> <li><a href="https://stackoverflow.com/questions/419163/what-does-if-name-main-do">What does <code>if __name__=="__main__":</code> do?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload