pabloem commented on a change in pull request #11943:
URL: https://github.com/apache/beam/pull/11943#discussion_r439075720



##########
File path: sdks/python/apache_beam/transforms/combiners.py
##########
@@ -66,8 +66,21 @@ class Mean(object):
   """Combiners for computing arithmetic means of elements."""
   class Globally(ptransform.PTransform):
     """combiners.Mean.Globally computes the arithmetic mean of the elements."""
+    def __init__(self, has_defaults=True, *args, **kwargs):
+      super(Mean.Globally, self).__init__()
+      self.has_defaults = has_defaults
+      self.args = args
+      self.kwargs = kwargs
+
     def expand(self, pcoll):
-      return pcoll | core.CombineGlobally(MeanCombineFn())
+      if self.has_defaults:
+        return pcoll | core.CombineGlobally(MeanCombineFn())
+      else:
+        return pcoll | core.CombineGlobally(MeanCombineFn()).without_defaults()
+
+    def without_defaults(self):
+      self.has_defaults = False

Review comment:
       what robert means in this case is that rather than mutating and 
returning self like so:
   ```
   def without_defaults(self):
     self.has_defaults = False
     return self
   ```
   
   It would be more useful to return a new instance with the new property. 
Somewhat like this:
   ```
   def with_defaults(self, has_defaults=True):
     return Mean.Globally(has_defaults=has_defaults, *self.args, **self.kwargs)
   ```
   It's useful to do this because then instance attributes are immutable, and 
it's easier to manage instances with immutable attributes.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to