Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Python has an empty constructor

Is it possible to build an empty constructor in Python? I'm in a class:

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

Now I initialise it as follows:

p = Point(0, 5, 10)

How do I make an empty constructor and initialise it as follows:

p = Point()

This is what I'm considering:

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

This works, but use caution. Any mutable types (objects, arrays, etc.) that are passed over will be shared by all instances.

According to this article, I should define the Point class's __init__() function with optional arguments. Is that right? I don't understand.

Sign In or Register to comment.