어쨌든, 아래의 노드 클래스에서 wordList 및 adjacencyList 변수는 Node의 모든 인스턴스간에 공유됩니다.파이썬 생성자 및 기본값
>>> class Node:
... def __init__(self, wordList = [], adjacencyList = []):
... self.wordList = wordList
... self.adjacencyList = adjacencyList
...
>>> a = Node()
>>> b = Node()
>>> a.wordList.append("hahaha")
>>> b.wordList
['hahaha']
>>> b.adjacencyList.append("hoho")
>>> a.adjacencyList
['hoho']
내가 생성자 매개 변수의 디폴트 값 (이 경우 빈 목록)을 계속 사용할 수 있지만, 자신의 단어 목록과 인접리스트 변수를 가지고 A와 B 모두를 얻을 수있는 방법이 있나요?
저는 python 3.1.2를 사용하고 있습니다.
가능한 중복 http://stackoverflow.com/questions/2681243/how-should-i-declare-default-values-for- instance-variables-in-python) –
파이썬에서 [최소 경악 (Least Astonishment)]의 가능한 복제본 : 어느 범위가 가변 기본값 인수입니까?] (http://stackoverflow.com/questions/1132941/least-astonishment-in-python -which-scope-is-the-mutable-default-argument-in) –