为什么不建议praxis通过传递实例变量来调用类方法?

2024-04-27 02:23:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用yield from关键字阅读this guide,在这里她插入了以下括号:

Note that it is not recommended practice to call a class method (self.child_iterate) by passing an instance variable (i.e., the self.left and self.right arguments) but that is a topic for a different post.

对我来说,不建议这样做的原因还不是很清楚,很想听听解释


Tags: tofromselfthatisnotit关键字
1条回答
网友
1楼 · 发布于 2024-04-27 02:23:32

从上下文来看,这不是100%清楚,但我认为这里提出的建议是,如果您需要将实例状态传递给类方法,那么它也可以是一个实例方法(如果您希望代码在外部可重用,那么可能只是调用类方法)。这降低了调用此方法的复杂性,因为您不必显式地传递实例数据

相关问题 更多 >