V
V
Vladimir Seregin2017-08-03 05:25:46
PHP
Vladimir Seregin, 2017-08-03 05:25:46

How to extend someone else's python library?

Noob and confusing question.
I have a library that makes a request to the API, it is used like this:

LOGIN = "user"
PASSWORD = "pass"
API_URL = "http://..."

api = library.Api(API_URL, LOGIN, PASSWORD)
#api.get(something)

I want to extend this library: write a get_something_specific function that does something based on api.get .
To use it like this:
#...
api = library.Api(API_URL, LOGIN, PASSWORD)
api.get_something_specific()

How to do this without changing the files of the library itself?
ps/ Google did not help, because I do not know what to look for.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Kolesnichenko, 2017-08-03
@Heavis

Create a class that inherits from library.Api. Or implement the desired logic as a separate function, not a method:

#...
api = library.Api(API_URL, LOGIN, PASSWORD)
get_something_specific(api)

V
Vlad Grigoriev, 2017-08-03
@Vaindante

You can use a decoy patch.
Rough example:

def get_something_specific(self, *args):
    pass

api.get_something_specific = get_something_specific

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question