pyunitwizard.has_unit#

pyunitwizard.has_unit(quantity_or_unit, target_unit, parser=None)[source]#

Check whether an object already uses an exact target unit.

This predicate inspects only unit metadata and never extracts or converts the magnitude. String quantities return None because answering for them requires parsing the input rather than inspecting existing metadata.

Parameters:
  • quantity_or_unit (QuantityOrUnit) – Quantity or unit whose current unit is inspected.

  • target_unit (str or UnitLike) – Exact unit expected on the input object.

  • parser (str, optional) – Parser used once when caching a string target unit.

Returns:

True for an exact unit match, False for a different unit, and None when the input is textual and cannot be inspected cheaply.

Return type:

bool or None

Examples

>>> import pyunitwizard as puw
>>> quantity = puw.quantity(1.0, "nanometer")
>>> puw.has_unit(quantity, "nm")
True