如何启动Python脚本中的几个函数

发布于 2024-09-02 15:03:09 字数 569 浏览 11 评论 0原文

我有一个 Python 脚本,我想在脚本中调用它的几个函数。下面的示例代码:

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()

我通过输入 ./name.py 来执行此代码。我如何从函数 printAddress() 到脚本末尾执行此代码?

谢谢

I have a Python script and I want to call it several functions down the script. 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. How could I exectute this code from the function printAddress() down the the end of the script?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂归处 2024-09-09 15:03:09

如果您问如何启动 python 脚本并让它在不同位置开始执行,那么您将必须启动脚本并提供一些有关您希望其执行的操作的信息。最常见的方法是添加对命令行参数的支持。

import sys

if __name__ == '__main__':   

    for arg in sys.argv: 
        print arg

如果您要从命令行单独执行上述脚本,它不会执行任何操作,但如果您使用一些额外的参数启动它,例如

./launch.py my_argument another_argument and_so_on

您将看到该脚本可以通过 sys.argv 列表访问额外的启动参数。使用它,您可以在启动时检查任何传递的参数,然后开始在所需位置执行脚本。

脚本的一个示例如下所示

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()

sys.argv[1:] 上的范围是因为 sys.argv 中的第一个条目将是启动脚本的路径。

因此,您可以启动此示例并获得以下结果

./launch
John
Place
100

./launch age
100

./launch address
Place

./launch name
John

现在这只是一个非常基本的示例。如果您决定朝这个方向走得更远,阅读 pythons getopt 可能会对您有用模块。它是命令行选项的解析器。

希望我正确理解了这个问题。

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. 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. 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.

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. It's a parser for command line options.

Hopefully I understood the question correctly.

累赘 2024-09-09 15:03:09

我不建议您实际上这样做,因为这是一个无限的递归函数,但它可以完成:)

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()

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()
因为看清所以看轻 2024-09-09 15:03:09

该行

Person = Name()

表明类的名称实际上应该是 Person。完全删除 printXXX 方法并添加 __repr__ 方法,例如

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

最后,我建议将姓名、地址和年龄添加到 init:

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

实例化一个 Person 对象后,你可以直接打印它:

person = Person()
print person

The line

Person = Name()

indicates that the class's name should actually be Person. Drop the printXXX methods entirely and add a __repr__ method instead, e.g. like

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:

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

After instantiating a Person object, you can just print it:

person = Person()
print person
生生不灭 2024-09-09 15:03:09

处理命令行选项的更灵活的方法是使用“optparse”模块。查看参考:http://docs.python.org/library/optparse.html

A more flexible way of handling command line options is to use the 'optparse' module. Check out the reference: http://docs.python.org/library/optparse.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文