Python 类内的 main 调用

发布于 2024-12-11 09:26:19 字数 461 浏览 3 评论 0原文

我没有做过太多Python工作——来自C/Java背景——所以请原谅我问这么一个简单的问题。我在 Eclipse 中使用 Pydev 来编写这个简单的程序,我想要它做的就是执行我的 main 函数:

class Example():

if __name__ == '__main__':
    Example().main()        <----- What goes here?


    def main(self):     
        print "Hello World!

这就是我现在所拥有的。我也尝试过

self.main() 

main()

main(self)

都不起作用。我缺少什么?

I haven't done much python - coming from a C/Java background - so excuse me for asking such a simple question. I am using Pydev in Eclipse to write this simple program, and all I want it to do is to execute my main function:

class Example():

if __name__ == '__main__':
    Example().main()        <----- What goes here?


    def main(self):     
        print "Hello World!

That is what I have now. I have also tried

self.main() 

and

main()

and

main(self)

none of which work. What am I missing?

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

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

发布评论

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

评论(3

强辩 2024-12-18 09:26:19

首先,您需要实际定义一个函数,然后才能运行它(并且不需要将其称为 main)。例如:

class Example(object):
    def run(self):
        print "Hello, world!"

if __name__ == '__main__':
    Example().run()

但是,您不需要使用类 - 如果您只想运行一些代码,只需将其放入函数中并调用该函数,或者只需将其放入 if 块:

def main():
    print "Hello, world!"

if __name__ == '__main__':
    main()

if __name__ == '__main__':
    print "Hello, world!"

Well, first, you need to actually define a function before you can run it (and it doesn't need to be called main). For instance:

class Example(object):
    def run(self):
        print "Hello, world!"

if __name__ == '__main__':
    Example().run()

You don't need to use a class, though - if all you want to do is run some code, just put it inside a function and call the function, or just put it in the if block:

def main():
    print "Hello, world!"

if __name__ == '__main__':
    main()

or

if __name__ == '__main__':
    print "Hello, world!"
长梦不多时 2024-12-18 09:26:19

整个块都放错了地方。

class Example(object):
    def main(self):     
        print "Hello World!"

if __name__ == '__main__':
    Example().main()

但你真的不应该使用类只是为了运行你的主代码< /a>.

That entire block is misplaced.

class Example(object):
    def main(self):     
        print "Hello World!"

if __name__ == '__main__':
    Example().main()

But you really shouldn't be using a class just to run your main code.

逐鹿 2024-12-18 09:26:19

请记住,您不可以这样做。

class foo():
    def print_hello(self):
        print("Hello")       # This next line will produce an ERROR!
    self.print_hello()       # <---- it calls a class function, inside a class,
                             # but outside a class function. Not allowed.

您必须从类外部或该类的函数内部调用类函数。

Remember, you are NOT allowed to do this.

class foo():
    def print_hello(self):
        print("Hello")       # This next line will produce an ERROR!
    self.print_hello()       # <---- it calls a class function, inside a class,
                             # but outside a class function. Not allowed.

You must call a class function from either outside the class, or from within a function in that class.

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