Hacker News new | past | comments | ask | show | jobs | submit login
Ask HN: How to build robust Python library?
5 points by huangruoyu on Nov 26, 2023 | hide | past | favorite | 3 comments
I have an idea for creating AI related python library. But I have been a library user so far, don't have a lot experience building libraries, like using meta-programming, special variables, library design pattern, etc. Is there good tutorials about building robust python library?



1. Pretend the library already exists.

2. Use your experience as a library user to write code using the library.

3. Implement the library so that the code from step 2 actually works.

If you discover that your implementation seems to require a lot of boilerplate and you suspect that metaprogramming could help simplify things, you can try it. Otherwise don't bother.


If you want to write a Python module, you need to be able to implement whatever you're imagining as plain python. A 2000 line python file is perfectly fine to start with.

If you can do that, making it into a proper module that you can pip install is just a matter of adding a pyproject.toml, adding an __init__.py, moving your functions into a new file and a few thing like that.

Generally you should not have any shared variables that are internal to the module (if that's what you're imagining.) If you don't know how to use advanced python constructs ypu don't need them.


One word of advice, the smaller your library, the fewer fires you'll have to put out in the future (bugs) and the lesser the energy spent on future rewrites




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: