Wesley Reynolds
Wesley Reynolds

Reputation: 11

Call class method from another class PYTHON

I am just trying to get a program that receives a point from one class, and then in another class, it uses that point as the center of the circle. I imagine this is simple but I don't know how to do it.

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

class Circle(Point):
     def circle(self, center, radius):
        Point.x = center
        Point.y = center
        self.radius = radius

Upvotes: 0

Views: 74

Answers (2)

Doum
Doum

Reputation: 1

The way you are doing it, with inheritance, is a bit confusing.

2 options are avalaible.

First : As mention by @Iain Shelvington, you could use the Point class as a member of your Circle class.

Second : If you really want to sub class it / inherit from the point in your circle, you have to super.init() it.

class Circle(Point):
    def __init__(self, x, y, radius):
        super().__init__(x, y) # which is the same as creating a self.x and y for Circle
        self.radius = radius

Upvotes: 0

Iain Shelvington
Iain Shelvington

Reputation: 32244

You shouldn't subclass Point for your Circle class, it doesn't make much sense as they are two completely different things. Instead you can take a Point as the center of your circle and pass it into the Circle class in the init

class Circle(object):
    def __init__(self, center: Point, radius):
        self.center = center
        self.radius = radius

Upvotes: 3

Related Questions