You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python调用类方法报错:Picture.GetFrameColour()缺少self参数

问题排查:Picture.GetFrameColour() missing required positional argument 'self'

错误核心原因

你初始化数组的代码myArray = [Picture for i in range(100)]是把Picture类本身重复100次存入数组,而非创建类的实例对象。后续遍历数组时,调用GetFrameColour()实际是在调用类的未绑定方法,这类方法必须手动传入self参数(即类实例),直接调用就会触发参数缺失的报错。

另外,ReadData()函数里用myArray.append(item)会在原有100个类对象的后面添加实例,导致数组前100项依然是类对象,遍历到这些项时必然报错。

额外潜在问题

  1. Windows文件路径中的反斜杠未转义,会被Python解析为转义序列(比如\U会触发Unicode转义错误)。
  2. 从文件读取的宽度、高度是字符串类型,直接和整数类型的maxwidth/maxheight比较会导致逻辑错误。
  3. 搜索条件逻辑写反:用户输入的是「最大宽高」,应该判断图片宽高小于等于最大值,而非大于等于。
  4. 存在变量名拼写错误(比如descrption、tempframcolour),可能引发未定义错误。

修复方案

步骤1:修正数组初始化

将预先填充类对象的数组改为空数组:

myArray = []

步骤2:修复文件路径

使用原始字符串避免转义问题:

filename = r"C:\Users\Mikaa\OneDrive\Documents\A2 python\pp solus x2\9618_41_on_21 V1\Pictures.txt"

步骤3:转换宽高为整数类型

读取文件时将宽度、高度转为整数:

width = int(lines[counter + 1].strip())
height = int(lines[counter + 2].strip())

步骤4:修正搜索条件逻辑

调整判断条件为图片宽高不超过用户输入的最大值:

if tempframecolour == framecolour and tempwidth <= maxwidth and tempheight <= maxheight:

步骤5:修正变量名拼写错误

统一变量名拼写,比如descrption改为description,tempframcolour改为tempframecolour。

完整修正代码

class Picture:
    # PRIVATE Pdescription : STRING
    # PRIVATE Pwidth : INTEGER
    # PRIVATE Pheight : INTEGER
    # PRIVATE Pframecolour : STRING
    def __init__(self, Pdescription, Pwidth, Pheight, Pframecolour):
        self.__Description = Pdescription
        self.__Width = Pwidth
        self.__Height = Pheight
        self.__FrameColour = Pframecolour

    def GetDescription(self):
        return self.__Description

    def GetWidth(self):
        return self.__Width

    def GetHeight(self):
        return self.__Height

    def GetFrameColour(self):
        return self.__FrameColour

    def SetDescription(self, newdescription):
        self.__Description = newdescription


myArray = []  # 初始化空数组


def ReadData():
    global myArray
    itemsadded = 0
    try:
        # 使用原始字符串避免转义问题
        filename = r"C:\Users\Mikaa\OneDrive\Documents\A2 python\pp solus x2\9618_41_on_21 V1\Pictures.txt"
        with open(filename, "r") as file:  # 使用with语句自动关闭文件
            lines = file.readlines()
            filelength = len(lines)
            for counter in range(0, filelength, 4):
                description = lines[counter].strip()  # 用strip()更简洁地去除换行符
                width = int(lines[counter + 1].strip())
                height = int(lines[counter + 2].strip())
                framecolour = lines[counter + 3].strip()

                item = Picture(description, width, height, framecolour)
                myArray.append(item)
                itemsadded += 1
    except IOError:
        print("file not found")

    return itemsadded


result = ReadData()
print(f"Loaded {result} pictures")

framecolour = input("Please enter the frame colour you are looking for: ").lower()
maxwidth = int(input("Please enter the maximum width you would like: "))
maxheight = int(input("Please enter the maximum height you would like: "))

for picture in myArray:  # 直接遍历数组元素,更简洁
    tempframecolour = picture.GetFrameColour().lower()  # 统一转为小写,避免大小写敏感
    tempwidth = picture.GetWidth()
    tempheight = picture.GetHeight()
    tempdescription = picture.GetDescription()
    if tempframecolour == framecolour and tempwidth <= maxwidth and tempheight <= maxheight:
        print(f"picture description: {tempdescription} | width: {tempwidth} | height: {tempheight}")

额外优化:使用strip()替代replace("\n", "")去除换行符更健壮;用with语句自动管理文件关闭;遍历数组时直接迭代元素而非用索引;将边框颜色转为小写后比较,避免大小写敏感问题。

内容的提问来源于stack exchange,提问作者Hamzah Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 03:55:28