我对 Python 中的默认参数感到困惑。这是我的代码:
#!/usr/bin/env python
import sys
class Node(object):
def __init__(self, ID=str(), items=[]):
self.ID = ID
self.items = items
if __name__ == '__main__':
a = Node('1')
b = Node('2')
b.items.append('sth.')
c = Node('3')
print a.items
print b.items
print c.items
The output is:
['sth.']
['sth.']
['sth.']
我只是更改 b 实例。为什么所有实例都更改了?