html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue https://github.com/pydata/xarray/issues/3829#issuecomment-597321847,https://api.github.com/repos/pydata/xarray/issues/3829,597321847,MDEyOklzc3VlQ29tbWVudDU5NzMyMTg0Nw==,14808389,2020-03-10T21:16:20Z,2020-03-10T23:19:20Z,MEMBER,":+1: We might want to discourage using this to add methods to the `DataArray` / `Dataset` namespace, though.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,575564170 https://github.com/pydata/xarray/issues/3829#issuecomment-597286480,https://api.github.com/repos/pydata/xarray/issues/3829,597286480,MDEyOklzc3VlQ29tbWVudDU5NzI4NjQ4MA==,2443309,2020-03-10T20:02:31Z,2020-03-10T20:02:31Z,MEMBER,Would it be worth adding @keewis' examples to the accessor documentation? ,"{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,575564170 https://github.com/pydata/xarray/issues/3829#issuecomment-594682466,https://api.github.com/repos/pydata/xarray/issues/3829,594682466,MDEyOklzc3VlQ29tbWVudDU5NDY4MjQ2Ng==,7441788,2020-03-04T17:27:09Z,2020-03-04T17:27:09Z,CONTRIBUTOR,"@keewis, thanks for the suggestions. Both seem reasonable. In your first example, if you wanted to prohibit `obj.weighted.sum(dim)`, you could just check for `self._weight` in `sum()`. Though I suppose it would be nice to be able to have the interpreter enforce the requirement and not have to do an explicit check in every method.","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,575564170 https://github.com/pydata/xarray/issues/3829#issuecomment-594670062,https://api.github.com/repos/pydata/xarray/issues/3829,594670062,MDEyOklzc3VlQ29tbWVudDU5NDY3MDA2Mg==,14808389,2020-03-04T17:10:59Z,2020-03-04T17:15:52Z,MEMBER,"not sure if you really should use accessors to do that; having a function return a wrapper object might be enough. Then again, an accessor is a slightly modified version of a property so you can just make your object callable by defining `__call__`: ```python @xr.register_dataarray_accessor(""weighted"") class Weighted: def __init__(self, xarray_obj): self._obj = xarray_obj self._weight = None def __call__(self, weight): self._weight = weight def sum(self, dim): return ""weighted sum"" ``` this does allow calling `obj.weighted.sum(dim)`, so instead you can use: ```python In [2]: class Weighted: ...: def __init__(self, obj, weight): ...: self._obj = obj ...: self._weight = weight ...: ...: def sum(self, dim): ...: return f""weighted sum over {dim} and with weight {self._weight}"" ...: ...: @xr.register_dataarray_accessor(""weighted"") ...: def weighted(obj): ...: def wrapped(weight): ...: return Weighted(obj, weight) ...: return wrapped ...: ...: da = xr.DataArray(data=range(5), dims=""x"") ...: da.weighted(5).sum(dim=""x"") Out[2]: 'weighted sum over x and with weight 5' ``` Edit: the warning about a overridden attribute was the result of rerunning the code without restarting the interpreter","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,575564170