私はPythonでグラフノードクラスを作成しました。
各ノードには、単一の親、複数の子およびプロパティがあります。
実装は以下のようにすべきである:このコードでpythonオブジェクトはinitialzeでプロパティを持っています
# graph_test.py
class Node(object):
def __init__(self, name, prop={}):
self.name = name
self.properties = prop
self.parent = None
self.children = []
print "New node:", self.name, self.properties
def add_prop(self, k, v):
self.properties.update({k:v})
print "added prop:", k, v
def add_child(self, n):
self.children.append(n)
n.parent = self
class Foo(object):
def __init__(self, n):
self.node_num = n
self.root_node = None
self.current_node = None
def bas(self):
n = Node("root")
n.add_prop("this_prop_is", "set_only_root_node")
self.root_node = n
return self.root_node
def bar(self):
self.current_node = self.bas()
for i in range(self.node_num):
n = Node(str(i))
self.current_node.add_child(n)
self.current_node = n
if __name__ == '__main__':
f = Foo(5)
f.bar()
、唯一のルートノードは、そのキー「this_prop_is」である特性を有していることが予想されます。
はしかし、実行結果は以下のようなものです:
$ python ./graph_test.py
New node: root {}
added prop: this_prop_is set_only_root_node
New node: 0 {'this_prop_is': 'set_only_root_node'}
New node: 1 {'this_prop_is': 'set_only_root_node'}
New node: 2 {'this_prop_is': 'set_only_root_node'}
New node: 3 {'this_prop_is': 'set_only_root_node'}
New node: 4 {'this_prop_is': 'set_only_root_node'}
すべてのノードでも、私が唯一のノード「ルート」に追加し、同じキーを持っています。
私はpython 2.7.6
を使用します。
私の質問は以下のとおりです。
- これはバグですか?
- これはバグではない場合、なぜこれが発生しますか?
- この問題を解決するにはどうすればよいですか?
ありがとうございました。 C++では、デフォルトの引数はすべての関数呼び出しで評価されるため、非常に混乱します... – furushchev