Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

how to instantiate an object within another object and then call methods from the second object (python)

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Shalini Jaiswal

User

( 5 months ago )


i have the following code:

class Commit:

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

class Branch:

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

class Repository:

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]

but when i do this to instantiate it:

    repo = Repository("syllabus 2.0")
    repo.branch("master").new_commit(Commit(message="add readme", changes=[("CREATE", "README.md")]))
    print(repo.branch("master").head)

it prints None Why is this happening?

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif