TERACytE
TERACytE

Reputation: 7863

How to add a method to an external class at runtime in Python?

PyGithub does not appear to support the GitHub API call to acquire the latest release. I'm wondering if I can add a method to PyGithub at run time that would do this for me.

For example, the existing code has the following method:

# Repository.py
def get_releases(self):
    """
    :calls: `GET /repos/:owner/:repo/releases <http://developer.github.com/v3/repos>`_
    :rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Tag.Tag`
    """
    return github.PaginatedList.PaginatedList(
        github.GitRelease.GitRelease,
        self._requester,
        self.url + "/releases",
        None
    )

I'd like to add this one to the Repository.py class:

def get_latest_release(self):
    return github.PaginatedList.PaginatedList(
        github.GitRelease.GitRelease,
        self._requester,
        self.url + "/releases/latest",
        None
    )

I tried this but I received an error:

# main.py

from types import MethodType
from github import Github
from github.Repository import Repository, github

def get_latest_release(self): ...

def main():
    Repository.method = MethodType(get_latest_release, None, Repository)
    g = Github(<my name>, <my password>)
    org = g.get_organization(<name of org>)
    repo = org.get_repo(<name of repository>)
    release = repo.get_latest_release()

    # AttributeError: 'Repository' object has no attribute 'get_latest_release'

Is it possible to add this method at runtime?

Upvotes: 0

Views: 158

Answers (1)

AChampion
AChampion

Reputation: 30258

Assigning directly to Repository.get_latest_release should be sufficients, e.g.:

Repository.get_latest_release = get_latest_release

You would only need MethodType if you were trying to assign to an instance, e.g.

>>> import types
>>> class A(object):
...     pass
>>> a = A()
>>> A.one = lambda self: 1
>>> a.two = types.MethodType(lambda self: 2, a)
>>> a.one(), a.two()
(1, 2)

Where class assignment is available to all instances of that class, instance assignment is not:

>>> b = A()
>>> b.one()
1
>>> b.two()
AttributeError: 'A' object has no attribute 'two'

Upvotes: 2

Related Questions