Answer the question
In order to leave comments, you need to log in
How to overload a class constructor in Python?
Given class:
class Container:
def __init__(self, a, b, c, d, e, f, g):
self.a = a
self.b = b
self.c = c
self.d = d
self.e = e
self.f = f
self.g = g
obj1 = Container(a=1, b=2, c=3, d=4)
obj2 = Container(x=4, y=3)
class Container:
def __init__(self, **kwargs):
for key in kwargs:
self.key = kwargs[key]
def get_data(request):
a = request.POST['a']
b = request.POST['b']
c = request.POST['c']
d = request.POST['d']
e = request.POST['e']
f = request.POST['f']
cont = Container(a, b, c, d, e, f)
return cont
send_data(instanceOfContainer)
send_data(a, b, c, d, e, f)
Answer the question
In order to leave comments, you need to log in
I did this:
class A(object):
def __init__(self, **kwargs):
for name, value in kwargs.items():
setattr(self, name, value)
a = A(x=4, y=5)
There is no such thing as "overload constructor" in Python. There is one __init__ , and to solve the problem with different situations, several methods are made, for example: SuperPuperClass.build_situation1(), SuperPuperClass.build_situation2(). You can read here: Python constructors and __init__
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question