0

I try to test my program to create a copy of an object and I get this error:

TypeError: __init__() takes 1 positional argument but 2 were given

I tried to check existing questions but I can't correct this error. Any suggestions?

This is my class:

class ordred_dict:
    #"""
    #This class is an ordred dictionary
    #composed of 2 listes: key list and value list as a dictionary
    #"""
    def __init__(self, orig):
    #"""
    #Constructur to initiate an empty key and value list 
    #"""
        self.key_list=list(orig.key_list)
        self.value_list=list(orig.value_list)
    def __init__(self, **Tuplekeysvalues):
        #"""
        #Create a new dict using a liste of (keys:values)
        #"""
        self.key_list=list()
        self.value_list=list()
        for key in Tuplekeysvalues:
            self.key_list.append(key)
            self.value_list.append(Tuplekeysvalues[key])
            #print("({}:{}) ".format(key, Tuplekeysvalues[key]))

#Main program
dict3=ordred_dict(p1="1",p2="2",p4="4",p3="3",p0="0")
dict2=ordred_dict(dict3)
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Sara
  • 37
  • 5
  • 1
    Do you know there's a [`collections.OrderedDict`](https://docs.python.org/2/library/collections.html#collections.OrderedDict) (also, note spelling...)? Also if you supply values by `**kwargs`, then **order is not guaranteed**. – jonrsharpe Jun 03 '15 at 18:09
  • I just start my first example on class in Python, I did not ordre the list elments yet. I get a problem when I copy dict3 in a new dict2. Any Idea? – Sara Jun 03 '15 at 18:11
  • 1
    Python classes cannot have multiple methods with the same name, so only the latter (which *takes no positional arguments*) exists. See http://stackoverflow.com/q/141545/3001761 for alternatives. – jonrsharpe Jun 03 '15 at 18:12
  • `p1="1",p2="2",p4="4",p3="3",p0="0"` what kind of structure is this? I cannot run your script with that input – heinst Jun 03 '15 at 18:12
  • 1
    @heinst they're `**kwargs` to the constructor – jonrsharpe Jun 03 '15 at 18:13
  • I am not using exsting OrdredDict, I try to create my own example. – Sara Jun 03 '15 at 18:13
  • P1, P2, .... are just an random data. I think it is not the same as C++, I'll put all args in the same function __init__ to avoid using same name. – Sara Jun 03 '15 at 18:15
  • *"I think it is not the same as C++"* well... no! – jonrsharpe Jun 03 '15 at 18:17
  • In the main, when I create new ordred_dict var without arg or using a liste of args it work even I have 2 methode __init__. It's only when I try to do a copy of the constructor I get the error. – Sara Jun 03 '15 at 18:18

1 Answers1

2

You can't overload constructors in Python like you can in some other languages. A better way of doing this would be something like:

class MyOrderedDict:
    def __init__(self, *args):
        self.key_list = []
        self.value_list = []
        for key,val in args:
            self.key_list.append(key)
            self.value_list.append(val)

    @classmethod
    def from_ordered_dict(cls, other):
        return cls(*zip(other.key_list, other.value_list))

Then call it with:

d = MyOrderedDict(('key1', 'value1'), ('key2', 'value2'))
e = MyOrderedDict.from_ordered_dict(d)
Adam Smith
  • 52,157
  • 12
  • 73
  • 112
  • @user405684 My code was giving the wrong output, but it *wasn't* empty! I've changed the code now so it should work perfectly. If not then maybe something else is wrong -- I can't reproduce your result. – Adam Smith Jun 03 '15 at 18:54