[Tutor] Instance into another instance
Gregor Lingl
glingl at aon.at
Sat Feb 19 23:36:00 CET 2005
Ismael Garrido schrieb:
> Hello.
>
> This is my code:
> class Node:
> def __init__(self, tag, value=None, **kwargs):
> self.tag = tag
> self.value = value
> self.kwargs = kwargs
> self.childs = []
>
> def addChild(self, tag, value=None, **kwargs):
> node = Node(tag, value, kwargs)
> self.childs.append(node)
>
> def __repr__(self):
> return "Tag %s ; value %s ; kwargs %s ; childs %s" % (self.tag,
> self.value, self.kwargs, self.childs)
> And this is what I do to test it:
> >>> ppal = Node("Test", value="Test")
> >>> ppal.addChild("test", value=2)
>
> Traceback (most recent call last):
> File "<pyshell#75>", line 1, in -toplevel-
> ppal.addChild("test", value=2)
> File "...node.py", line 10, in addChild
> node = Node(tag, value, kwargs)
> TypeError: __init__() takes at most 3 arguments (4 given)
This means, afaik, __init__() takes at most 3 non-keyword arguments
(plus an arbitrary number of keyword-arguments).
You passed 4 non-keyword - arguments, the first beeing (implicitely)
self (i. e. node), and then three more: tag, value, kwargs.
(In your case kwargs is the empty dictionary.)
I suppose you wanted to pass addChild's **kwargs
to Node's **kwargs, which goes like this:
def addChild(self, tag, value=None, **kwargs):
node = Node(tag, value, **kwargs)
self.childs.append(node)
HTH,
Gregor
>
> I don't understand. Why 4 arguments are given? What can I do to solve that?
>
> The idea of the class is to be able to create a "tree". Where each node
> can have subnodes, which in turn can have their subnodes...
>
> Thanks
> Ismael
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
--
Gregor Lingl
Reisnerstrasse 3/19
A-1030 Wien
Telefon: +43 1 713 33 98
Mobil: +43 664 140 35 27
Autor von "Python für Kids"
Website: python4kids.net
More information about the Tutor
mailing list