Python version: 3.10.0

I am working on a tooling set for some LaTex formatting with mathematics content. As part of this, I would have created a class for fractions that are not being simplified, yet still have most of the functionality of the Fraction class from the fractions module. This is a completely custom and unique class.

fraction_tools.py

class UnsimplifiedFraction:
    # A completely custom class, not inheriting from Fraction, that has similar functionality.
    # i.e. has addition, subtraction, multiplication and division but **does not simplify**.

In another, more functional, file I am utilising these fractions within a more complex construction:

do_stuff.py

from .fraction_tools import UnsimplifiedFraction
...
...
        result = [
            [
                UnsimplifiedFraction(a1_1, FRAC),
                UnsimplifiedFraction(a1_2, FRAC),
            ],
            [
                UnsimplifiedFraction(a2_1, FRAC),
                UnsimplifiedFraction(a2_2, FRAC),
            ]
        ]
...
...

However, within the do_stuff (and many other) file(s) I have other requirements to use the Fraction class as well as the UnsimplifiedFraction class. It seems more reader friendly to include the import once in the fraction_tools.py file, but Fraction will not actually be used within that file (leading to a linting error).

Is there a preference between from fractions import Fraction in multiple modules, or importing it once in fraction_tools.py and then from .fraction_tools import Fraction, UnsimplifiedFraction?
i.e.:

Option a

fraction_tools.py

from fractions import Fraction  # linting error for unused import


class UnsimplifiedFraction:
    # A completely custom class, not inheriting from Fraction, that has similar functionality.
    # i.e. has addition, subtraction, multiplication and division but **does not simplify**.

do_stuff.py

from .fraction_tools import Fraction, UnsimplifiedFraction
...
...
# code requiring both Fraction and UnsimplifiedFraction
...
...

Option b

fraction_tools.py

class UnsimplifiedFraction:
    # A completely custom class, not inheriting from Fraction, that has similar functionality.
    # i.e. has addition, subtraction, multiplication and division but **does not simplify**.

do_stuff.py

from fractions import Fraction  # not as nice to read and required in multiple files
from .fraction_tools import UnsimplifiedFraction
...
...
# code requiring both Fraction and UnsimplifiedFraction
...
...

I have considered updating the UnsimplifiedFraction class for this specific instance, but two problems exist:

  • I am not sure how to approach this in the timescale available.
  • The general concept is still a question for me; is it a good practice to import similar pre-existing classes into an overall helper module and then import from that helper, or should the import happen in multiple places?

Either code works, I just don't know which is preferred or advised (against!).

0

There are 0 best solutions below