0

I have been given these two class definitions:

class Weird(object):
    def __init__(self, x, y): 
        self.y = y
        self.x = x
    def getX(self):
        return x 
    def getY(self):
        return y

class Wild(object):
    def __init__(self, x, y): 
        self.y = y
        self.x = x
    def getX(self):
        return self.x 
    def getY(self):
        return self.y

X = 7
Y = 8

The first two questions were fine; they simply asked

w2 = Wild(X, Y)
print(w2.getX())

(which is 7) and

print(w2.getY())

What confuses me are the questions

w1 = Weird(X, Y)
print(w1.getX())

and

print(w1.getY())

both of whose answers are apparently "error", but I don't understand why this is the case. The errors given are "x is not defined" and "y is not defined" respectively, but I thought x and y are the parameters that we put into the function. What am I missing?

alexqwx
  • 147
  • 1
  • 7

1 Answers1

0

In your first class, x and y are constructor arguments. They are not accessible out of this scope.

In the getX function, x doesn't exists. You need to retrieve it from the self reference, such as in your second class.

Kmaschta
  • 2,369
  • 1
  • 18
  • 36