I'm beginner in Python and I have problem to run my function in command line, through IDLE it's working, but I need to run it in command line, because I will have to make from it executable file...
So my problem... I have this
file name test.py
class A
def myFunction(a,b)
print a,b
myFunction calls Class, from IDLE it's enough to write myfunction(a,b), but from command line I don't know what to do
My goal is
to run function from command line with command: name_of_the_file arg1 arg2 --> test a b
I looked I think everywhere and tried everything, most common what I found was to add to my function this command
if __name__ == '__main__':
a = sys.argv[0]
b = sys.argv[1]
myFunction(a,b)
So it was
class A:
some process which calls myFunction
def myFunction(a,b)
print a,b
if __name__ == '__main__':
a = sys.argv[0]
b = sys.argv[1]
myFunction(a,b)
and then I called it from command line with test a b, but I got only errors
I use Python 2.7
Thank you for your help
-
1docs.python.org/2/tutorial/index.htmlIgnacio Vazquez-Abrams– Ignacio Vazquez-Abrams2013年12月31日 16:06:01 +00:00Commented Dec 31, 2013 at 16:06
-
If you put a function inside a class then you need an instance of that class to call that function (if I understand your question correctly)Jason Sperske– Jason Sperske2013年12月31日 16:06:55 +00:00Commented Dec 31, 2013 at 16:06
-
your stuff doesn't compile because there are missing colons and whitespace. What is your question exactly?Vorsprung– Vorsprung2013年12月31日 16:10:19 +00:00Commented Dec 31, 2013 at 16:10
2 Answers 2
Some issues with your code:
- Python is case-sensitive. The keyword is
class, notClass. - After the line
class Athere has to be a colon:class A: If the function
myFunctionis supposed to be part ofclass A, it has to be indented:class A: def myFunction(a,b)Methods of classes should have
selfas first parameter:def myFunction(self, a, b)- After
def myFunction(self, a,b)there has to be a colon:def myFunction(self, a,b): Your function must have at least one line of indented code following. If it is supposed to do nothing, you can use the keyword `pass:
def myFunction(self, a,b): passIf you want to use
sys.argvyou first have to importsysat the beginning of your code withimport sys.myFunctionis part of a class, you first have to instantiate it to use the function:Av = A() Av.myFunction(a,b)The first commandline argument is the second entry of
sys.argv, not the first.
However it seems to me that you don't want a class anyway, so just write:
def myFunction(a,b):
pass
if __name__ == '__main__':
a = sys.argv[1]
b = sys.argv[2]
myFunction(a,b)
Also you call python scripts with python file.py arg1 arg2. If you want to omit python at the beginning then you can (in unix-like systems) add a shebang in the first line of the python-file: #!/usr/bin/env python. Then as long as the execution flag is set chmod +x file.py it may be called like ./file.py arg1 arg2.
4 Comments
Functions and Methods are 2 different things. Read more about it here. Methods must be called with their parent class as the first argument:
class Fruit:
def Taste(self):
print "Yuck!"
# Call it
Fruit().Taste()
You could pass argv in this manner:
MyClass(*args, **kwargs).MyMethod(argv, **kwargs)
EDIT
Am I right in assuming that you'd like to pass 2 (or more) arguments to the command line so that they could be passed on to a function and executed? If yes, I'll try something simple here:
from sys import argv
try:
a = argv[1]
b = argv[2]
except IndexError:
print "Enter both arguments"
class Integer:
def Check(self, z):
if int(z) < 0:
print z, "is a negative integer"
elif int(z) > 0:
print z, "is a positive integer"
else:
print z, "is not an integer"
# Make instance
myclass = Integer()
# Call methods
myclass.Check(a)
myclass.Check(b)