I am trying to extend a class from a given library but I do not have access to the __init__
of the class, only to a function generating an instance. Something like
class A:
pass
def return_an_A():
return A()
class B(A):
# How to instantiate B with only access to return_an_A
def extend_A():
pass
How can I define the instanciation of class B?
Thanks for your help.
Update
As rightfully noticed, my example was poorly set up so here is, I hope, a better explanation of my real issue.
The original code I used was the following.
import gitlab
# Here gl.projects.get(project_name) is an instance of the class
# gitlab.v4.objects.Project
def project(url, private_token, project_name):
with gitlab.Gitlab(url, private_token) as gl:
return gl.projects.get(project_name)
# This implementation takes an instance of gitlab.v4.objects.Project
def list_files(project, commit_branch):
current_files = []
if not project.empty_repo:
current_files = [
f["path"]
for f in project.repository_tree(
ref=commit_branch
)
]
return current_files
I wanted to have a structure like
class MyProject:
# Here is missing the way to instantiate like the project function
# I don't want to pass a Project instance as a parameter to make it an
# attribute, I would like to extend the class Project itself
def list_files(self, commit_branch):
current_files = []
# Note here that the variables of the
# gitlab.v4.objects.Project are directly accessible
if not self.empty_repo:
current_files = [
f["path"]
for f in self.repository_tree(
ref=commit_branch
)
]
return current_files
but I can't manage to find the right way to write the __init__
.