【发布时间】:2015-04-30 01:39:42
【问题描述】:
我的问题是:当我测试我的“isProduct”方法时,我收到错误消息:
TypeError: isProduct() takes exactly 2 arguments (1 given)
所以,我寻找这个问题,我发现我必须在调用我的方法之前添加'self'。我做到了。但是,它仍然说:
NameError: name 'self' is not defined
不要介意方法说什么,我的问题涉及属性、类和自我。 这是我的代码,我在做什么(非常)错?
import xlrd
wb=xlrd.open_workbook('C:\\Inputs_UNF_newForm.xlsx')
s=wb.sheet_by_name('Chemical treatments')
p=wb.sheet_by_name('Products')
class World:
def RowMatrix(self,sheet_name,matrix_name):
sheet=wb.sheet_by_name(sheet_name)
number_of_rows = sheet.nrows
for row in range(number_of_rows):
value = str((sheet.cell(row,0).value))
if value == "#" +matrix_name:
start=row
if value !="":
end=row+1
return (start,end)
def isProduct(self,look_for):
(start,end)= World.RowMatrix("Products","Products")
number_of_columns=p.ncols
for row in range(start,end):
for col in range(number_of_columns):
value = str((sheet.cell(row,col).value))
if value == look_for:
return true
else:
return false
if self.isProduct("K20"):
print("true")
else:
print("false")
【问题讨论】:
-
看看this tutorial关于Python中的类和对象。