Python 2.7 从python中的方法返回错误字符串

Python 2.7 从python中的方法返回错误字符串,python-2.7,class,methods,except,raise,Python 2.7,Class,Methods,Except,Raise,我在读一个类似的问题。当我尝试在面向对象编程中创建一些类似的东西,这样我就可以学到更多的东西时,我迷失了方向 我正在使用Python2.7,我是面向对象编程的初学者 我想不出如何使它工作 示例代码checkArgumentInput.py: #!/usr/bin/python __author__ = 'author' class Error(Exception): """Base class for exceptions in this module.""" pass

我在读一个类似的问题。当我尝试在面向对象编程中创建一些类似的东西,这样我就可以学到更多的东西时,我迷失了方向

我正在使用Python2.7,我是面向对象编程的初学者

我想不出如何使它工作

示例代码checkArgumentInput.py:

#!/usr/bin/python

__author__ = 'author'


class Error(Exception):
    """Base class for exceptions in this module."""
    pass


class ArgumentValidationError(Error):
    pass

    def __init__(self, arguments):
        self.arguments = arguments

    def print_method(self, input_arguments):
        if len(input_arguments) != 3:
            raise ArgumentValidationError("Error on argument input!")
        else:
            self.arguments = input_arguments
            return self.arguments
#!/usr/bin/python

__author__ = 'author'


class ArgumentLookupError(LookupError):
    pass

    def __init__(self, *args): # *args because I do not know the number of args (input from terminal)
        self.output = None
        self.argument_list = args

    def validate_argument_input(self, argument_input_list):
        if len(argument_input_list) != 3:
            raise ValueError('Error on argument input!')
        else:
            self.output = "Success"
            return self.output
在main.py脚本上:

#!/usr/bin/python
import checkArgumentInput

__author__ = 'author'


argsValidation = checkArgumentInput.ArgumentValidationError(sys.argv)

if __name__ == '__main__':

    try:
        result = argsValidation.validate_argument_input(sys.argv)
        print result
    except checkArgumentInput.ArgumentValidationError as exception:
        # handle exception here and get error message
        print exception.message
当我执行main.py脚本时,它会生成两个空行。即使我没有提供任何参数作为输入,或者即使我提供了参数输入

所以我的问题是如何让它工作

我知道有一个模块可以通过检查参数输入来为我完成这项工作,但我想实现一些我也可以在其他情况下使用的东西(尝试,除非)


提前感谢您花时间和精力阅读并回答我的问题。

好的。因此,通常调用函数
sys.argv[]
,函数末尾用括号括起来,括号之间用数字括起来,如:
sys.argv[1]
。此函数将读取命令行输入。Exp.:
sys.argv[0]
是文件名

main.py 42
在本例中,
main.py
是sys.argv[0],而
42
sys.argv[1]

您需要标识要从命令行中获取的字符串。 我认为这就是问题所在


更多信息:

我做了一些研究,发现了这个有用的问题/答案,帮助我理解我的错误:

我在下面发布了正确的功能代码,以防将来有人会从中受益

示例代码checkArgumentInput.py:

#!/usr/bin/python

__author__ = 'author'


class Error(Exception):
    """Base class for exceptions in this module."""
    pass


class ArgumentValidationError(Error):
    pass

    def __init__(self, arguments):
        self.arguments = arguments

    def print_method(self, input_arguments):
        if len(input_arguments) != 3:
            raise ArgumentValidationError("Error on argument input!")
        else:
            self.arguments = input_arguments
            return self.arguments
#!/usr/bin/python

__author__ = 'author'


class ArgumentLookupError(LookupError):
    pass

    def __init__(self, *args): # *args because I do not know the number of args (input from terminal)
        self.output = None
        self.argument_list = args

    def validate_argument_input(self, argument_input_list):
        if len(argument_input_list) != 3:
            raise ValueError('Error on argument input!')
        else:
            self.output = "Success"
            return self.output
第二部分main.py:

#!/usr/bin/python
import sys
import checkArgumentInput

__author__ = 'author'

argsValidation = checkArgumentInput.ArgumentLookupError(sys.argv)

if __name__ == '__main__':

    try:
        result = argsValidation.validate_argument_input(sys.argv)
        print result
    except ValueError as exception:
        # handle exception here and get error message
        print exception.message
以下代码打印:
参数输入错误如预期,因为我违反了条件


无论如何,谢谢你们所有人的时间和努力,希望这个答案将来能帮助其他人。

你好,最危险的人,我稍后会回复你们。不幸的是,我现在无法访问脚本。再次感谢您花时间和精力阅读和回答我的问题。再次您好,最危险的,很抱歉这么晚回复,但我正忙于其他事情。不幸的是,你的答案不正确。关于sys.argv[0]和sys.argv[1],您是对的,但是我的代码有很多错误。我花了一些时间,我找到了解决办法。我现在会发布我问题的答案,所以将来可能会有人从中受益。我会从中受益,谢谢。我说,将来会帮助我帮助别人。谢谢你的回复。