diff options
author | Hernan Grecco <hernan.grecco@gmail.com> | 2020-02-21 23:41:43 -0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-21 23:41:43 -0300 |
commit | 78b1e52958a326cba4983c3ef84016756274c92c (patch) | |
tree | dfcaa026318ffb0c06fc82729955da8479c581c2 /pint/util.py | |
parent | 36ebb0f0e280bbb5b1cd32475e4b13d0222a371c (diff) | |
parent | 92dc58e7b1de6a7ad0aaaad48f63490a80e1d82d (diff) | |
download | pint-_decimal.tar.gz |
Merge branch 'master' into _decimal_decimal
Diffstat (limited to 'pint/util.py')
-rw-r--r-- | pint/util.py | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/pint/util.py b/pint/util.py index 68bed2e..aeebff1 100644 --- a/pint/util.py +++ b/pint/util.py @@ -897,12 +897,16 @@ def infer_base_unit(q): def getattr_maybe_raise(self, item): - """Helper function to invoke at the beginning of all overridden ``__getattr__`` - methods. Raise AttributeError if the user tries to ask for a _ or __ attribute. + """Helper function invoked at start of all overridden ``__getattr__``. + + Raise AttributeError if the user tries to ask for a _ or __ attribute, + *unless* it is immediately followed by a number, to enable units + encompassing constants, such as ``L / _100km``. Parameters ---------- - item : + item : string + Item to be found. Returns @@ -911,7 +915,11 @@ def getattr_maybe_raise(self, item): """ # Double-underscore attributes are tricky to detect because they are # automatically prefixed with the class name - which may be a subclass of self - if item.startswith("_") or item.endswith("__"): + if ( + item.endswith("__") + or len(item.lstrip("_")) == 0 + or (item.startswith("_") and not item.lstrip("_")[0].isdigit()) + ): raise AttributeError("%r object has no attribute %r" % (self, item)) |