diff --git a/tensorflow_probability/python/internal/structural_tuple.py b/tensorflow_probability/python/internal/structural_tuple.py index 9199a7dd1e..c9aafc9101 100644 --- a/tensorflow_probability/python/internal/structural_tuple.py +++ b/tensorflow_probability/python/internal/structural_tuple.py @@ -142,7 +142,9 @@ def __getnewargs__(self): """Return self as a plain tuple. Used by copy and pickle.""" return tuple(self) - def __getattr__(self, attr): + def __getattribute__(self, attr): + if attr.startswith('_'): + return super().__getattribute__(attr) attr_idx = self._field_name_to_index.get(attr) if attr_idx is None: raise AttributeError('StructTuple has no attribute {}'.format(attr)) diff --git a/tensorflow_probability/python/internal/structural_tuple_test.py b/tensorflow_probability/python/internal/structural_tuple_test.py index 1fd536a106..23348c2882 100644 --- a/tensorflow_probability/python/internal/structural_tuple_test.py +++ b/tensorflow_probability/python/internal/structural_tuple_test.py @@ -100,6 +100,10 @@ def foo(a, b): self.assertEqual(3, nest_util.call_fn(foo, t(1, 2))) + def testTupleMethods(self): + t = structural_tuple.structtuple(['count', 'a', 'b']) + self.assertEqual(t(1, 2, 3).count, 1) + def testMoreThan255Fields(self): num_fields = 1000 t = structural_tuple.structtuple(