Search code examples
python-3.xoopinstance

instance variable and data attribute


class Coordinate (object):
    def __init__ (self, x, y):
        self.x = x
        self.y = y

c = Coordinate (3,4)
print (c.x)

in this code, is c.x instance variable ? and self.x is data attribute, right ?


Solution

  • class Coordinate (object):
        def __init__ (self, x, y):
            self.x = x
            self.y = y
    
    c = Coordinate (3,4)
    print (c.x)
    

    In the above code :

    1. self.x and self.y are attributes of the class

    2. c.x and c.y are the instance variables. All the instance variables have different values for different instances.

    3. Actual parameters are the parameters which you specify when you call the Functions. A formal parameter is a parameter which you specify when you define the function. The actual parameters are passed by the calling function. The formal parameters are in the called function. Thus (3,4) here are the actual parameters, whereas the variables used in the function definition which later will hold the values of the actual parameters are termed as formal parameters. Here self.x and self.y are formal parameters.