Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Python
  1  class cl:
  2      def __init__(self,x,y):
  3          self.x = x
  4          self.y = y
  5      def multiply(self,n):
  6          self.x = self.x * n
  7          self.y = self.y * n
  8  
  9  class Cam(cl):
 10      def __init__(self,g,x,y):
 11          self.g = g
 12          super().__init__(x,y)
 13  
 14  
 15  if __name__ == "__main__":

the ouput:
m.x is : 2 ## m.y is : 3
    m = Cam(1,2,3)
    print('m.x is :',m.x,'##','m.y is :' ,m.y)


What I have tried:

I am trying let my code print:
m.x is : 2 ## m.y is : 3

it does it correctly, but I noticed when I exchange x and y in the line 12 it gives me the output:
m.x is : 3 ## m.y is : 2

even though they remain in the line 10 in the order g,x,y.

Do you know why and which mechanism stands behind it (or what happens behind the scenes?)
Posted
Updated 10-Jul-20 1:20am
v2

1 solution

Of course it does, as you are passing parameters by position and not by name. So if you reverse the order in line 12 then they will be reversed when they are received by c1's constructor.
If you want the values to be referred to by name then your code needs to be:
Python
class cl:
    def __init__(self,x=0,y=0):
        self.x = x
        self.y = y
    def multiply(self,n):
        self.x = self.x * n
        self.y = self.y * n

class Cam(cl):
    def __init__(self,g,x=0,y=0):
        self.g = g
        super().__init__(x=x, y=y) # the order for x and y can be changed
 
Share this answer
 
Comments
Ahmad Qassym 11-Jul-20 11:27am    
thanx mr MaCutchan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900