问题:我在程序中调用类时遇到问题。
我创建了一个名为example.py的程序,其中包含以下代码:
stage('tests') {
parallel(
'Unit Tests': {
node('unitNode') {
ws('/path/to/workspace') {
new file("${env.WORKSPACE}/package.json") // does not work
sh 'cat $WORKSPACE/package.json' // works OK
}
}
},
'E2E Tests': {
node('e2eNode') {
ws('/path/to/workspace') {
new file("${env.WORKSPACE}/package.json") // does not work
sh 'cat $WORKSPACE/package.json' // works OK
}
}
}
)
}
我试图在另一个代码为
的程序中调用ExampleBaseclass ExampleBase:
"""
This is the ExampleBase class
"""
def __init__(self, company_name="N/A", stock_dict={}):
"""
class constructor
"""
self.company_name = company_name
self.stock_dict = stock_dict
return
def __str__(self):
"""
Prints the company name string
"""
str = "The Company name is: %s" %\
(self.company_name
)
return str
def add_purchase(self, addtlSTK):
"""
Adds item to stock_dict
"""
self.stock_dict.update(addtlSTK)
return
答案 0 :(得分:1)
您的代码唯一的问题是example.py的错误缩进。方法必须在类下缩进。还要确保两个python文件都在同一个文件夹中。
这是缩进的example.py,
class ExampleBase:
"""
This is the ExampleBase class
"""
def __init__(self, company_name="N/A", stock_dict={}):
"""
class constructor
"""
self.company_name = company_name
self.stock_dict = stock_dict
return
def __str__(self):
"""
Prints the company name string
"""
str = "The Company name is: %s" % \
(self.company_name
)
return str
def add_purchase(self, addtlSTK):
"""
Adds item to stock_dict
"""
self.stock_dict.update(addtlSTK)
return
答案 1 :(得分:0)
好的,在检查缩进后,构造函数似乎运行正常。我现在正试图做更多事情并且崩溃。这是我试图运行的代码:
import example
if __name__ == "__main__":
a = {"10-01-2014":(10, 11.25), "10-02-2014":(11, 12.25), "10-03-2014":(12, 13.25)}
b = example.ExampleBase("Bern", a)
c = {"10-04-2014":(13, 14.25)}
b.example.ExampleBase.add_purchase(c)
print(b)
我现在看到的错误是: AttributeError:'ExampleBase'对象没有属性'example'