Home python python classes inheritance

python classes inheritance

Author

Date

Category

I have 2 class

Class Point:
  Def __init __ (Self, X, Y):
    Self.x = X.
    self.y = y.
  DEF DIST (SELF, POINT):
    distance = ((self.x - point.x) ** 2 + (self.y - point.y) ** 2) ** 0.5
    RETURN DISTANCE
  DEF X (SELF):
    Return Self.x.
  DEF Y (Self):
    Return Self.y.
Class Circle (Point):
  Def __init __ (Self, R, Point):
    Self.r = R.
    Self.point = Point
  DEF Center (Self):
    Return Self.point.

I do not understand how to implement such a design to applying the X property of the Radius property of the Circle object, I received the value x, and when referring to y, respectively y. To create an object C1 C1 = Circle (4, Point (1.5.1)) , it was possible to get the X C1.center.x == 1.5


Answer 1

You have Center is a method. To get a value from it, it is not enough to contact him by name, it still needs Call . For this you need to add brackets after his name:

c1.center (). X

however, there is an alternative. You can use the method of Property method, and then it will start behaving as an ordinary attribute, and it will be possible to call it as you are in the example

Class Circle (Point):
  Def __init __ (Self, R, Point):
    Self.r = R.
    Self.point = Point
  @property # add this
  def center (self):
    return self.point
c1 = Circle (4, Point (1.5,1))
print (c1.center.x) # And this option starts

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions