简体   繁体   中英

NameError: name 'self' is not defined: Python

I have two programs in python. The Program contains a class and function init and second program is for calling the program1.

I am getting below error which is mentioned in the screenshot. enter image description here

Here are the two programs: f1.py


class Getdata:
def __init__(self):
    self.IMAGE_SIZE = 112
    self.IMAGES_LENGTH = 150
    self.call_data()
def call_data(self):
    x = 10
    y = self.IMAGES_LENGTH
    z = self.IMAGE_SIZE
    return x,y,z

mainclass.py


from f1 import Getdata
x_train, y_train, z_train = Getdata.call_data(self)
print(x_train, y_train, z_train)

You firstly need to instantiate the class, and you do not have to pass the self parameter when calling a class function, modify f1.py to look like this:

from f1 import Getdata
getdata = Getdata()
x_train, y_train, z_train = getdata.call_data()
print(x_train, y_train, z_train)

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