简体   繁体   中英

Class does not pass the self variables to functions inside of it

I am a newbie programmer I am defining this simple class but I receive the following error I am not able to understand what im doing wrong

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 )

When I call NAME = '002498'

PreProcessing.image_loader(NAME)

, i get this error:

---------------------------------------------------------------------------
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'

As @kindall said in his comment, you aren't making an instance of your class. It'll work if you set it up like this, creating a "hello" object of class PreProcessing():

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM