Editing Python libraries

I have been using Python for a fairly long time and now I want to modify a Python library to fit my needs.

There are two ways to do this as far as I know. One is to modify the source code itself. Other is to write a wrapper around the original library.

But I don’t understand which is better, doing what is a good practice editing source or writing a wrapper? And in which situation one is preferred over other?


It mostly depends on how deep are your changes. Wrappers can bring additional behavior, but can rarely change the existing one. For instance, if a target library writes some data to a file and you want, instead, to send this data to a database, unless the library was designed to allow that, you won’t be able to change the behavior just with a wrapper.

  • If the changes are shallow and additive, use a wrapper. You may need a wrapper anyway if you want to be able to move to a different library later without having to modify a large part of your application.

  • If the changes are deep and substantial:

    1. Do a fork on GitHub if the library is hosted there, or contact the author of the library if it is hosted somewhere else.

    2. Make the change.

    3. Share the change with the author and the community: if you needed a different behavior, there are chances other people do need it too.

When changing an existing library, if the original author considers that your changes shouldn’t be merged and should stay as a fork, make sure you create your own pip package. This would not only simplify the deployment of this library for you, but also make it possible for other people to use your fork instead of the original library if they need the modified behavior.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *