类不会将自变量传递给其内部的函数

时间:2018-07-15 23:30:12

标签: python python-3.x

我是新手程序员 我正在定义这个简单的类,但是收到以下错误 我不明白我在做什么错

from PIL import Image
class PreProcessing(object):
    def __init__(self,NAME):
        super(PreProcessing,self).__init__()
        self.name = NAME
        self.newsize = 512
        PATH = '/home/alireza/Desktop/ssd'
        self.pathImage =  PATH + '/' + self.name + '.jpg'
        self.pathAnn = PATH + '/' + self.name + '.xml'



    def image_loader(self):
        print(self.pathImage )

当我打电话时 NAME = '002498'

PreProcessing.image_loader(NAME)

,出现此错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-38-5747710fa005> in <module>()
      3 sizee = [1, 3, 375, 500]
      4 # A= PreProcessing(NAME)
----> 5 PreProcessing.image_loader(NAME)

<ipython-input-37-5f788218f7e3> in image_loader(self)
     10 
     11     def image_loader(self):
---> 12         print(self.pathImage )

AttributeError: 'str' object has no attribute 'pathImage'

1 个答案:

答案 0 :(得分:1)

正如@kindall在他的评论中所说,您不是在创建类的实例。如果这样设置,它将创建PreProcessing()类的“ hello”对象,它将起作用:

from PIL import Image
class PreProcessing(object):
    def __init__(self,NAME):
        super(PreProcessing,self).__init__()
        self.name = NAME
        self.newsize = 512
        PATH = '/home/alireza/Desktop/ssd'
        self.pathImage =  PATH + '/' + self.name + '.jpg'
        self.pathAnn = PATH + '/' + self.name + '.xml'



    def image_loader(self):
        print(self.pathImage )

NAME = "12345"

hello = PreProcessing(NAME)