如何启动Python脚本中的几个函数
我有一个 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您问如何启动 python 脚本并让它在不同位置开始执行,那么您将必须启动脚本并提供一些有关您希望其执行的操作的信息。最常见的方法是添加对命令行参数的支持。
如果您要从命令行单独执行上述脚本,它不会执行任何操作,但如果您使用一些额外的参数启动它,例如
您将看到该脚本可以通过 sys.argv 列表访问额外的启动参数。使用它,您可以在启动时检查任何传递的参数,然后开始在所需位置执行脚本。
脚本的一个示例如下所示
sys.argv[1:] 上的范围是因为 sys.argv 中的第一个条目将是启动脚本的路径。
因此,您可以启动此示例并获得以下结果
现在这只是一个非常基本的示例。如果您决定朝这个方向走得更远,阅读 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.
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
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
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
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.
我不建议您实际上这样做,因为这是一个无限的递归函数,但它可以完成:)
I would not recommend you'd actually do this, as it's an endless recursive function this way, but it can be done:)
该行
表明类的名称实际上应该是
Person
。完全删除printXXX
方法并添加__repr__
方法,例如最后,我建议将姓名、地址和年龄添加到
init:
实例化一个
Person
对象后,你可以直接打印它:The line
indicates that the class's name should actually be
Person
. Drop theprintXXX
methods entirely and add a__repr__
method instead, e.g. likeFinally, I recommend adding name, address and age to the parameter list accepted by
init
:After instantiating a
Person
object, you can just print it:处理命令行选项的更灵活的方法是使用“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