DEV Community

PyBash
PyBash

Posted on

Where should I put this?😕

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?

Top comments (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 •

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 •

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 •

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
 
phlash profile image
Phil Ashby •

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

Collapse
 
pybash profile image
PyBash •

hmmm....

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay