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
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
to run function from command line with command: name_of_the_file arg1 arg2 --> test ab
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 ab, but I got only errors
I use Python 2.7
Thank you for your help
Some issues with your code:
class
, not Class
. class A
there has to be a colon: class A:
If the function myFunction
is supposed to be part of class A
, it has to be indented:
class A: def myFunction(a,b)
Methods of classes should have self
as first parameter: def myFunction(self, a, b)
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): pass
If you want to use sys.argv
you first have to import sys
at the beginning of your code with import sys
.
myFunction
is 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
.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.