DHI / DHI/python-package-development

Add section on inappropriate intimacy between classes

未关闭
#35 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
Jupyter Notebook
星标
8
派生
1
平均合并
4 分钟
30 天内合并 PR
1

描述

Classes should interact through public APIs, not by reaching into each other's private attributes.

**Example from MIKE IO plotting refactor:**

Before (inappropriate intimacy):
```python
# Plotter reaches into geometry internals for subsetting
values = values[self.da.geometry.top_elements]
geometry = self.da.geometry.geometry2d

# Plotter uses private attribute to check for time axis
if self.da._has_time_axis:
return self.da.values[0]
```

After (using public API):
```python
# Use sel/isel for subsetting
da = da.sel(layers="top")
geometry = da.geometry

# Use public dims property
da = self.da.isel(time=0) if "time" in self.da.dims else self.da
```

**Key principles:**
- Never access private attributes (prefixed with `_`) of another class
- Use public methods like `sel`, `isel`, and public properties like `dims`
- If you need to access private state, that's a signal the class is missing a public API
- Plotters should plot, not subset — keep data manipulation in the data layer

贡献指南

这个仓库没有索引到贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。