课程&功能 -

时间:2017-12-18 02:35:10

标签: python function class instance

我不确定自己到底做错了什么,因为我几乎绝对肯定我已经引用了变量而且都是正确的。

我使用函数相当新,并且刚开始学习使用Python类。

因此,当我运行代码时,我收到此错误消息:

Random rand = new Random();
String s = IntStream.generate(() -> rand.nextInt(26)).limit(75)
        .mapToObj(i -> Character.toString(
                rand.nextBoolean() ? (char) (i + 'a') : (char) (i + 'A')))
        .collect(Collectors.joining());
System.out.println(s.substring(0, 25));
System.out.println(s.substring(25, 50));
System.out.println(s.substring(50));

我猜测它与我的顺序代码步骤有关,或者是因为我已经将numfiles输入转换为代码第20行中的int()。

我已在下面附上我的代码。请提前帮助我:)

line 37, in pathlist
    while self.no_of_files > 0:             #self.number_of_files
AttributeError: 'int' object has no attribute 'no_of_files'

3 个答案:

答案 0 :(得分:0)

你应该在创建时给出no_of_files参数  通过调用def init (self,no_of_files)来计算Calculate_RMSE_Assess_Models实例。

答案 1 :(得分:0)

您需要将self添加到number_of_files()out_path()open_read_write_file()的签名中:

class Calculate_RMSE_Assess_Models:

   def __init__(self, no_of_files):
       self.no_of_files = no_of_files

   def number_of_files():
      numfiles = input("Enter the number of files to iterate through: ")
      numfilesnumber = int(numfiles)
      return numfilesnumber

   def pathlist(self):
      filepathlist = []
      while self.no_of_files > 0:             #self.number_of_files
          path = input("Enter the filepath of the input file: ")
          filepathlist.append(path)
          no_of_files = no_of_files - 1
      return filepathlist

    def out_path(self):
      path = input("Enter the file path of output path: ")
      return path


    def open_read_write_files(self):
       pass

但是,如果您希望保留类中函数的属性,可以使用classmethod装饰器:

class Calculate_RMSE_Assess_Models:

   def __init__(self, no_of_files):
       self.no_of_files = no_of_files
   @classmethod
   def number_of_files(cls):
      numfiles = input("Enter the number of files to iterate through: ")
      numfilesnumber = int(numfiles)
      return numfilesnumber

   def pathlist(self):
      filepathlist = []
      while self.no_of_files > 0:             #self.number_of_files
          path = input("Enter the filepath of the input file: ")
          filepathlist.append(path)
          no_of_files = no_of_files - 1
      return filepathlist
    @classmethod
    def out_path(cls):
      path = input("Enter the file path of output path: ")
      return path

    @classmethod
    def open_read_write_files(cls):
       pass

答案 2 :(得分:0)

在您的班级定义中,您有list_filepath = pathlist(no_of_files)。这是pathlistno_of_filesselfno_of_filesint,因此while self.no_of_files > 0:正在尝试访问no_of_files的{​​{1}}属性。

完整的Traceback显示了这一点。在查看这样的问题时发布完整的Traceback很有帮助。

int