【发布时间】:2015-08-29 18:03:18
【问题描述】:
我有以下代码:
类提交:
id = 0
commits = []
def __init__(self, message=None, changes=None):
Commit.id += 1
self.id = Commit.id
self.message = tuple([message])
self.changes = tuple(changes)
Commit.commits.append(self.message + self.changes)
self.commits = Commit.commits
self.next = None
类分支:
def __init__(self):
self.tail = None
self.head = None
def new_commit(self, commit):
if not self.head:
self.head = commit
self.tail = self.head
else:
self.tail.next = commit
self.tail = self.tail.next
类存储库:
branches = {}
def __init__(self, name):
self.name = name
self.branches = Repository.branches
def branch(self, branch_name):
self.branch_name = Branch()
self.branches[branch_name] = self.branch_name
return self.branches[branch_name]
但是当我这样做来实例化它时:
repo = Repository("syllabus 2.0")
repo.branch("master").new_commit(Commit(message="add readme", changes=[("CREATE", "README.md")]))
print(repo.branch("master").head)
它打印无 为什么会这样?
【问题讨论】:
-
您创建一个新的
Branch每次调用.branch- 为什么?! -
.branch 应该在我每次调用 .branch 时在该分支名称下创建一个新的分支实例
-
但这没有任何意义——你怎么能回到以前的版本?每次访问给定分支时,它都是一个全新的对象。
-
是的,我意识到了这一点,正因为如此,当我调用分支时它无法工作,感谢您的帮助。
标签: object python-3.x data-structures linked-list