2

I got class Z which has 5 attributes:

  • ID
  • actual x
  • actual y
  • future x
  • future y

actual x and actual y I can replace using Position class(using class inheritance). The problem is that I want to replace future position using same method. How can I do it to avoid namespace conflict? Do I really need to create new Position class with slightly changed namespace ?

I know I can do (but i dont like this solution):

class Position: def __init__(self,x , y): self.x = x self.y = y class Position2: def __init__(self,x , y): self.fx = x self.fy = y class Z(Position,Position2): def __init__(self, x, y, f_x, f_y, Id): super().__init__(x, y) super().__init__(f_x,f_y) self.Id = Id 

actual code:

class Position: def __init__(self,x , y): self.x = x self.y = y class Z(Position): def __init__(self, x, y, f_x, f_y, Id): super().__init__(x, y) self.Id = Id 

I would like to get something like:

class Position: def __init__(self,x , y): self.x = x self.y = y class Z(Position): def __init__(self, x, y, f_x, f_y, Id): super().__init__(x, y) super().__init__(f_x, f_y) self.Id = Id 

that I could easy print actual and future parameters.

3
  • What is Z? Is it a thing with two positions? or a Position with two positions? Commented Aug 2, 2019 at 23:24
  • why are you even trying to use inheritance here? Just use composition Commented Aug 3, 2019 at 0:14
  • i wanted to do it neatly/clearly Commented Aug 5, 2019 at 19:53

3 Answers 3

4

It looks like you are looking for composition rather than inheritance. Position is a concept. You have a class that references two positions:

class Position: def __init__(self, x, y): self.x = x self.y = y class Z: def __init__(self, x, y, fx, fy, id): self.p = Position(x, y) self.fp = Position(fx, fy) self.id = id 

You should only inherit from Position if you want Z to have access to the behavior of Position. If that were the case, you could either define fx and fy as attributes of Z, or use composition as before:

class Z(Position): def __init__(self, x, y, fx, fx, id): super.__init__(x, y) self.fx = fx self.fy = fy self.id = id 

Or

class Z(Position): def __init__(self, x, y, fx, fx, id): super.__init__(x, y) self.fp = Position(fx, fy) self.id = id 

By the way, if your Position class is really just a simple container, consider using collections.namedtuple to hold the values. For example:

Position = namedtuple('Position', ['x', 'y']) 

This version of Position inherits from the built in tuple, so it is much more efficient at storing values.

Sign up to request clarification or add additional context in comments.

Comments

0

why not list in Position constructor,

class Position: def __init__(self,x , y): self.x_lst.append(x) self.y_lst.append(y) class Z(Position): def __init__(self, x, y, f_x, f_y, Id): super().__init__(x, y) super().__init__(f_x, f_y) self.Id = Id 

x_lst and y_lst are lists. Not sure if this idea works for you.

3 Comments

You'll get an AttributeError if you try to run this. x_lst and y_lst need to be defined.
second init accesses the same class, he has mult inheritence here
@snamef. Multiple inheritance is when your class inserts from two classes. This only inherits from one. __init__ is just a regular method in this context, and you can call it as often as you want. The second call just overwrites the attributes set by the first one.
0

You can also call Type.init(self,args) for a parent class init

class First(object): def __init__(self, x): print ("First(): entering") self. x = x print ("First(): exiting") class Second(object): def __init__(self , y ): print ("Second(): entering") self. y = y print ("Second(): exiting") class Third(First, Second): def __init__(self,x,y): print ("Third(): entering") First.__init__(self,x) Second.__init__(self,y) #super(Third, self).__init__(x,y) print ("Third(): exiting") def __repr__(self): return f'{self.x} { self.y}' a = Third(10,20) print(a) 

2 Comments

OP has a solution with three classes but stated they did not want the third class.
This is a correct solution (excerpt the indentation), but does not capture the concept of OP's intent.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.