0

I would like to create a circle class with three parameters (radius, coordinate x and y).

class circle:
  def __init__(self, r, x, y): 
    self.radius = r
    self.X = x
    self.Y = y

Calling it requires 3 arguments(radius, x and y). Apart from taking 3 arguments like normal, how do I make it to be able to take 1 argument(the radius) and take 2 arguments(coordinate x and coordinate y) at the same time?

For instances, the class can take all of these at the same time:

uk = circle(4, 5, 6) #(r, x, y)
uj = circle(1, 2) #(x, y)
uh = circle(8) #(r)

Could it be dealt with multiple constructors(overloading, classmethod)?

PypypieYum
  • 37
  • 5
  • 2
    Python **does not support method overloading**. Instead, you can 1) Write a signature that could accept any of these and disambiguate the case inside `__init__` 2) use alternative constructors -- probably using `classmethod`. – juanpa.arrivillaga Mar 01 '22 at 17:40
  • This [question](https://stackoverflow.com/questions/6434482/python-function-overloading) has various useful answers. – quamrana Mar 01 '22 at 17:42
  • Note you should probably have a single `position` parameter that's a sequence `(x, y)` - those things _belong_ together. – jonrsharpe Mar 01 '22 at 17:44

0 Answers0