【发布时间】:2018-06-02 23:28:00
【问题描述】:
我最近正在通过某些在线课程学习 Python 课程,所以我完全是 Python 新手。
版本 [1]
class Bubble:
def __init__(self, pos, vel, colour):
self.pos = list(FIRING_POSITION)
self.vel = [0, 0]
self.color = random.choice(COLOR_LIST)
版本 [2]
class Bubble:
def __init__(self):
self.pos = list(FIRING_POSITION)
self.vel = [0, 0]
self.color = random.choice(COLOR_LIST)
我的疑问是:
(1) 比较Version [1]和Version [2],不知道什么时候需要放pos、等变量>vel 和 colour 转换为 __init__(self) 作为 __init__(self, pos, vel, colour)
(2) 这两个版本有什么区别?
【问题讨论】:
-
在 (1) 中,您没有使用您传入的任何参数,因此它们毫无意义 - 您不需要传入参数,除非您需要将它们用于某些事情(它是与任何其他功能没有什么不同)。否则,它们都会“工作”。
-
感谢您的快速回复!
标签: python initialization arguments init