DEV Community

loading...

Where should I put this?😕

PyBash
I am a developer from India. I use Python to develop my software/tools. I make software/tools - ranging from mini CLI-full GUI. I think these tools help in making life easier. Love Web dev. as well.
・1 min read

I need a small help!

Well, just now I wanted to add a new function to my python library - py-everything! But I am not sure in which module I should put it? First I thought, ok, it should go into maths. But then I thought it is not really a mathematical operation, you are checking if a number is even or not? I Will, be happy if you helped!

The function takes in a number and returns True if it is even, False otherwise.

So, should I put it in maths or __init__ - the base module?

Discussion (8)

Collapse
codemouse92 profile image
Jason C. McDonald • Edited

__init__ shouldn't usually contain implementations, so I'd put it in maths.

Then, if you want mylib.maths.is_number to be available as mylib.is_number, you'd put this in __init__.py...

import .maths.is_number as is_number
Enter fullscreen mode Exit fullscreen mode

That will allow you to import it from your library like this:

from mylib import is_number
Enter fullscreen mode Exit fullscreen mode
Collapse
pybash profile image
PyBash Author

Yes, but it is a very common function so, I would want so that it can be imported easily.

Collapse
codemouse92 profile image
Jason C. McDonald

Ergo, the preceding suggestion. You still don't want to put implementation in __init__.py if you can at all help it.

Thread Thread
pybash profile image
PyBash Author

Hmm, I also don't want to, but I want the function to be accessible much easier, you see?

Thread Thread
codemouse92 profile image
Jason C. McDonald • Edited

That's why I suggested what I did. By following the technique I outlined, you can access the function from just mylib instead of mylib.maths. Read my initial comment again carefully.

Thread Thread
pybash profile image
PyBash Author

Yep, your 1st comment explains it... I will try that, it looks good to me. But again, I have to see what others, comment, right?

Collapse
phlash909 profile image
Phil Ashby

Look like a mathematical concept to me... :)
basic-mathematics.com/even-and-odd...

Collapse
pybash profile image
PyBash Author

hmmm....