I have a class A that can be generated from two different ways.
- a = A(path_to_xml_file)
- a = A(listA, listB)
The first method has file path as an input to parse from XML file to get listA, and listB. The second method is given two lists.
I can think of two ways to implement multiple constructor. What do you think? What method normally Python guys use for this case?
Check the type
class A():
def __init__(self, arg1, arg2 = None):
if isinstance(arg1, str):
...
elif isinstance(arg1, list):
...
a = A("abc")
b = A([1,2,3],[4,5,6])
Make different builders
class A2():
def __init__(self):
pass
def genFromPath(self, path):
...
def genFromList(self, list1, list2):
...
a = A2()
a.genFromPath("abc")
b = A2()
b.genFromList([1,2,3],[4,5,6])