Python 引用其他.py文件中的类和类的方法
#HelloWorld是文件名称,Hello是类
from HelloWorld import Hello
调用,Hello类的方法:
>>> h = Hello()>>> h.hello()
Hello, world #输出结果
#>>> Hello().hello()
#Hello, world
附:HelloWorld.py文件内容
#
class Hello(object):
def hello(self,name= 'world'):
print('Hello, %s' %name)
本文介绍如何在Python中从一个模块导入另一个模块的类并使用其方法。通过具体实例演示了如何从HelloWorld模块导入Hello类,并调用其hello方法。
2573

被折叠的 条评论
为什么被折叠?



