Is there any way to create an empty constructor in python. I have a class:
class Point:
def __init__(self, x, y, z):
self.x = x
self.y = y
self.z = z
now I initialize it like this:
p = Point(0, 5, 10)
How can I create an empty constructor and initialize it like this:
p = Point()
class Point:
def __init__(self):
pass
As @jonrsharpe said in comments, you can use the default arguments.
class Point:
def __init__(self, x=0, y=0, z=0):
self.x = x
self.y = y
self.z = z
Now you can call Point()
You can achieve your desired results by constructing the class in a slightly different manner.
class Point:
def __init__(self):
pass
def setvalues(self, x, y, z):
self.x = x
self.y = y
self.z = z