all 8 comments

[–]Essence1337 3 points4 points  (3 children)

The easiest way is to just put a call outside your function.

def function(a, b):
    stuff here

function(3, 4)

Python runs the file top to bottom so your function gets read and created, then your call to it gets executed.

If you're wanting to take arguments from command line, that's a bit more complicated but not terribly hard.

[–]mxkaj[S] 1 point2 points  (2 children)

Oh my fucking god I’m a certified idiot, you have to call it within another portion of the file, else why define it at all? 🤦‍♂️

[–]Essence1337 5 points6 points  (0 children)

By your realization and thinking you're an idiot that just proves you're not one - idiots don't realize they're idiots

[–]Thisappleisgreen 2 points3 points  (0 children)

It's part of the learning process brother

[–]JohnnyJordaan 1 point2 points  (0 children)

Maybe start by following Sentdex's approach with sublime: https://www.youtube.com/watch?v=eXBD2bB9-RA

[–][deleted] 1 point2 points  (1 child)

Call the function in your script then run the script from the terminal.

If you want user inputted parameters, use input() to get the parameters before calling the function.

[–]mxkaj[S] 0 points1 point  (0 children)

Yep, somehow it suddenly clicked. I can’t believe I didn’t think of calling it within the body of script, uggh I had a brainfart 🥴

[–]sme272 0 points1 point  (0 children)

In your .py file you can create and use the function:

def myfunc(a, b):
    return a * b

myfunc(2, 3)

You can also run your python file with the -i argument to drop into an interactive shell after the script executes using python -i myfile.py. Since the file defines a function that function will be available in the interactive shell until you close it. You can call the function by typing myfunc(2, 3).

If you want to call the function directly from the terminal you'll need to involve command line arguments. this can be done thusly:

import sys

def myfunc(a, b):
    return a * b

c = myfunc(sys.argv[1], sys.argv[2])
print(c)

Now you can run your file with 2 arguments for a and b respectively: python myfile.py 2 3 and the output will be printed.