Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python empty constructor

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() 
like image 956
Narek Tarasyan Avatar asked Mar 19 '17 09:03

Narek Tarasyan


People also ask

Can you have an empty constructor in Python?

This is because there is a default constructor implicitly injected by python during program compilation, this is an empty default constructor that looks like this: def __init__(self): # no body, does nothing. In this case, python does not create a constructor in our program.

Is there default constructor in Python?

In Python the __init__() method is called the constructor and is always called when an object is created. Types of constructors : default constructor: The default constructor is a simple constructor which doesn't accept any arguments.

How do you define an empty init in Python?

You should define the __init__() method of your Point class with optional parameters. This is the best answer. It covers all of the ground that the original question asks about.

Is __ init __ constructor?

"__init__" is a reserved method in python classes. It is known as a constructor in OOP concepts. This method called when an object is created from the class and it allows the class to initialize the attributes of a class.


1 Answers

class Point:     def __init__(self):         pass 
like image 160
Waxrat Avatar answered Sep 22 '22 14:09

Waxrat