0

I have come across the notion of class constructors in C++. But I have not yet found a way to initialize a class in Python using 2 or more different methods. Could anyone tell how to go about that?

8
  • 1
    Look at __init__ and __new__ in the data model google.com/search?client=firefox-b-e&q=python+init+vs+new. Usually in Python you don't implement the constructor (__new__) but just use __init__ to initalise a new instance of your class. Commented Jun 2, 2020 at 15:46
  • 1
    Are you asking about constructors in general, or are you looking for an analog to C++'s overloaded constructors in Python? Commented Jun 2, 2020 at 15:48
  • docs.python.org/3/tutorial/classes.html This is the standard way to go about constructors in general. Here is also a nice explanation youtube.com/watch?v=ZDa-Z5JzLYM. I hope this helps! :) Commented Jun 2, 2020 at 16:00
  • Python does not have method overloading if it is what you are looking for. But you can have different class methods acting like a factory, that is, with different names, accepting their own argument list and returning object instances of the same or different type. Commented Jun 2, 2020 at 16:04
  • @progmatico but that would not be like what init provides, right? I don't want to create instances of car using methods car1 and car2. Commented Apr 12, 2021 at 17:40

2 Answers 2

2

You don't need multiple constructors in python, you can use the following way to initialize if you have multiple such a case

class A:
    def __init__(self, arg_1, arg_2=None):
        self.arg_1 = arg_1
        self.arg_2 = arg_2

So when you need to initialize an object of class A, you can use

a1 = A(2)
a2 = A(2, 4)

Though strictly speaking __init__ is not a constructor but an initialiser

Sign up to request clarification or add additional context in comments.

1 Comment

If it is a question of having a few optional arguments, of course this a valid choice. See help(int.from_bytes) for an example of what I meant in the comment above, giving an alternate method to int()
0

@Ratan Rithwik solution is correct but only only 2 cases

If you want to have as many case as you want, you can use **kwarg
one example with @thebjorn answer

EDIT: mixing 'standard' parameter (having default value) and kwargs

class Player:
    def __init__(self, name='John', **kwargs):
        self.name = name
        self.last_name = kwargs.get('last_name')


p = Player(last_name='Doe')
print (p.name) # John
print (p.last_name) #Doe

p1 = Player('foo')
print (p1.name) # foo
print (p1.last_name) #None

2 Comments

Can I provide default values there somehow? Assume None might be a potential acceptable value
yes, you can use 'standard' parameter with default value : see my edit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.