[Tutor] Trying to understand how object re-instantiation gets propagated
Dimitar Ivanov
dimitarxivanov at gmail.com
Wed Feb 17 06:43:11 EST 2021
Hi all,
Apologies if this isn't the correct place to ask, I'm running into a rather
curious situation that I'm trying to fully grasp and so far my logic is
failing me so I hope maybe you could help clear up my confusion. :)
I have a small Python program that works with JIRA issues, I've created my
own class that works on top of the Python jira package
<https://pypi.org/project/jira/> as means to have easier access to the
fields I require. While this setup isn't directly related to my question
and I suspect you could easily recreate my situation with any OOP setup, I
feel like it's necessary to mention what I'm doing.
My JIRA issue class (shown below) retrieves the values from a JIRA server
and saves them as instance variables. To avoid outdated information when I
update a value on the JIRA server, I made a small function that retrieves
the JIRA issue from the server again and re-instantiates a new object of my
class:
def main():
jira_issue = jira_connection.issue("ISSUE-1") # Retrieving the issue
from the JIRA server
my_issue = JiraIssue(jira_issue)
print(my_issue.assignee)
# Output is "Unassigned"
# Go in JIRA and change the assignee to 'Foobar' now
my_issue.reload()
print(my_issue.assignee)
# Output now is "Foobar"
class JiraIssue:
def __init__(self, jira_issue):
self.key = issue.key
self.assignee = issue.fields.assignee
def reload(self):
jira_issue = jira_connection.issue(self.key) # Retrieving the issue
from the JIRA server
issue = JiraIssue.__init__(self, jira_issue)
return issue
At this point, my logic tells me that once I have invoked
'my_issue.reload()', I should have assigned its return value to a variable
in order to gain access to the object that was re-instantiated but instead
Python just "knows" that my_issue is now referring to the newly
instantiated object by the reload() method.
So, my question is - how does this happen? How does the interpreter know of
the new object and automatically assigns it to my_issue?
Thanks a lot in advance! :)
Regards,
Dimitar
More information about the Tutor
mailing list