简体   繁体   English

从另一个文件调用方法AttributeError: object has no attribute

[英]Calling a method from another file AttributeError: object has no attribute

I'm trying to create a unit test and I'm having some errors when I try to use a function from another file.我正在尝试创建一个单元测试,但当我尝试使用另一个文件中的 function 时遇到了一些错误。 You can see an example of the basic idea of what I am trying to do below.您可以在下面看到我正在尝试做的基本想法的示例。 I am a beginner in Python so I'm not sure what's the problem.我是 Python 的初学者,所以我不确定是什么问题。

In file 1: model.py在文件 1 中:model.py

Class Model(parameters):

      def calc_maximum(self, data, thresholds): 
          df['Max']= ...
          return df

      def calc_model_output(self,data,param,param2): 
          S=self.calc_maximum(data,thresholds)
          Si=S+param2
          return Si
          

in file 2: test.py在文件 2 中:test.py

import model as ml
import unittest 
...
...

class Tests(unittest.Testcase):
      def test_calc_maximum(self):
          Expected1=ml.Model.calc_maximum(self,input1, input2)
      def test_calc_model_output(self): 
          Expected2=ml.Model.calc_model_output(self,input1,input2,input3) 

when I try to run test.py file, the test for Expected1 seems to work ok, but I get the following error from the Expected2 row: AttributeError: 'Tests' object has no attribute 'calc_maximum .当我尝试运行 test.py 文件时,对Expected1的测试似乎工作正常,但我从Expected2行收到以下错误: AttributeError: 'Tests' object has no attribute 'calc_maximum

Any ideas?有任何想法吗?

Just add brackets after Model in Expected1, ie make Expected1 Expected1中的Model后面加上括号即可,即make Expected1

Expected1=ml.Model().calc_maximum(input1,input2)

Do the same for Expected2对 Expected2 做同样的事情

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:'int'对象没有属性'insert'; 定义和调用递归方法 - AttributeError: 'int' object has no attribute 'insert'; Defining and calling a recursive method 我收到此错误 AttributeError: 'function' object has no attribute 'hlauncher' while试图从另一个文件中获取属性 - I got this error AttributeError: 'function' object has no attribute 'hlauncher' while trying to get a attribute from another file 得到 AttributeError: 'module' 对象在从另一个文件导入全局变量时没有属性 - got AttributeError: 'module' object has no attribute while importing global variable from another file AttributeError:从文件读取时,“ str”对象没有属性“ readline” - AttributeError: 'str' object has no attribute 'readline' While Reading from File AttributeError: 'PostDetailView' 对象没有属性 'method - AttributeError: 'PostDetailView' object has no attribute 'method AttributeError: 'function' 对象没有属性 'method' - AttributeError: 'function' object has no attribute 'method' AttributeError 'class name' object 没有属性 'method' - AttributeError 'class name' object has no attribute 'method' AttributeError: 'method' object 没有属性 'backend' - AttributeError: 'method' object has no attribute 'backend' AttributeError:'str'对象没有属性(方法) - AttributeError: 'str' object has no attribute (method) AttributeError:“请求”对象没有属性“方法” - AttributeError: 'Request' object has no attribute 'method'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM