简体   繁体   English

如何启动Python脚本中的几个功能

[英]How to start a Python script several functions in

I have a Python script and I want to call it several functions down the script. 我有一个Python脚本,我想在脚本中将其称为几个功能。 Example code below: 下面的示例代码:

class Name():

    def __init__(self):
        self.name = 'John'
        self.address = 'Place'
        self.age = '100'

    def printName(self):
        print self.name

    def printAddress(self):
        print self.address

    def printAge(self):
        print self.age

if __name__ == '__main__': 
    Person = Name()
    Person.printName()
    Person.printAddress()
    Person.printage()

I execute this code by entering ./name.py . 我通过输入./name.py执行此代码。 How could I exectute this code from the function printAddress() down the the end of the script? 如何从脚本末尾的函数printAddress()提取此代码?

Thanks 谢谢

If you are asking how can you launch your python script and have it start executing at different positions then you will have to launch the script with some information on what you want it to do. 如果您问如何启动python脚本并使其在不同位置开始执行,那么您将必须启动脚本并附带一些有关您要执行的操作的信息。 The most common way to do this would be to add support for command line arguments. 最常见的方法是添加对命令行参数的支持。

import sys

if __name__ == '__main__':   

    for arg in sys.argv: 
        print arg

If you were to execute the above script from the command line by itself it would not do anything, but if you launch it with some extra parameters such as 如果要单独从命令行执行上述脚本,则不会执行任何操作,但是如果使用一些额外的参数(例如:

./launch.py my_argument another_argument and_so_on

You will see the script has access to the extra launch arguments through the sys.argv list. 您将看到脚本可以通过sys.argv列表访问额外的启动参数。 Using this, you can check for any passed args on launch and then start executing your script at your desired location. 使用此功能,您可以检查启动时是否传递了所有的参数,然后在所需的位置开始执行脚本。

One example with your script could be as follows 您的脚本的一个示例如下所示

import sys

class Name:

    def __init__(self):
        self.name = 'John'
        self.address = 'Place'
        self.age = '100'

    def printName(self):
        print self.name

    def printAddress(self):
        print self.address

    def printAge(self):
        print self.age


if __name__ == '__main__': 

    Person = Name()

    launchOptions = sys.argv[1:]

    if not launchOptions or 'name' in launchOptions:
        Person.printName()

    if not launchOptions or 'address' in launchOptions:
        Person.printAddress()

    if not launchOptions or 'age' in launchOptions:
        Person.printAge()

The range on the sys.argv[1:] is because the first entry in the sys.argv will be the path to the launched script. sys.argv [1:]上的范围是因为sys.argv中的第一个条目将是启动脚本的路径。

So you could launch this example and get the following results 因此,您可以启动此示例并获得以下结果

./launch
John
Place
100

./launch age
100

./launch address
Place

./launch name
John

Now this is just a very basic example. 现在,这只是一个非常基本的示例。 If you are decide to go further in this direction it may be useful for you to read up on pythons getopt module . 如果您决定朝这个方向走,那么对python的getopt模块进行阅读可能对您很有用。 It's a parser for command line options. 它是命令行选项的解析器。

Hopefully I understood the question correctly. 希望我正确理解了这个问题。

I would not recommend you'd actually do this, as it's an endless recursive function this way, but it can be done:) 我不建议您实际执行此操作,因为这是一种无穷的递归函数,但是可以做到这一点:)

class Name():

    def __init__(self):
        self.name = 'John'
        self.address = 'Place'
        self.age = '100'

    def printName(self):
        print self.name

    def printAddress(self):
        print self.address
        main()

    def printAge(self):
        print self.age

def main():
    Person = Name()
    Person.printName()
    Person.printAddress()
    Person.printage()

if __name__ == '__main__': 
    main()

The line 线

Person = Name()

indicates that the class's name should actually be Person . 指示该类的名称实际上应为Person Drop the printXXX methods entirely and add a __repr__ method instead, eg like 完全删除printXXX方法,并添加__repr__方法,例如

def __repr__(self):
    return "%s\n%s\n%s" % (self.name, self.address, self.age)

Finally, I recommend adding name, address and age to the parameter list accepted by init : 最后,我建议将名称,地址和年龄添加到init接受的参数列表中:

def __init__(self, name, address, age):
    ...

After instantiating a Person object, you can just print it: 实例化一个Person对象之后,您可以只打印它:

person = Person()
print person

A more flexible way of handling command line options is to use the 'optparse' module. 处理命令行选项的一种更灵活的方法是使用“ optparse”模块。 Check out the reference: http://docs.python.org/library/optparse.html 查看参考: http : //docs.python.org/library/optparse.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM