我尝试从单独的文件中调用python中的方法:(下面的示例)
#File1.py
from selenium import webdriver
class Myclass1():
try:
------
except Exception as e:
print(e)
------
def ab(self):
--------
def cd(self):
--------
然后我需要将ab和cd方法调用到文件#2
#File2.py
from file1 import Myclass1
def test1()
callfromfile1 = Myclass1()
callfromfile1.ab()
callfromfile1.cd()
if __name__== "__test__":
main()
习题: 当我尝试运行file2.py时 它只运行try和except,但它不运行方法ab和cd。 感谢
答案 0 :(得分:0)
编写适当的python代码如下: -
File1.py
class Myclass1():
try:
print 'try and catch function()'
except:
print 'exception'
def ab(self):
print 'ab()'
def cd(self):
print 'cd()'
File2.py
from File1 import Myclass1
def test1():
callfromfile1 = Myclass1()
callfromfile1.ab()
callfromfile1.cd()
if __name__== "__main__":
test1()
输出: -
try and catch function()
ab()
cd()