【发布时间】:2018-10-11 16:24:07
【问题描述】:
在类中创建变量并在__init__ 中为其赋值与直接在__init__ 中创建变量之间有什么区别?
例如:
示例 1:
# I create the var color outside __init__ and assigns a value to it inside __init__
class Car:
color = ""
def __init__(self):
self.color = "green"
示例 2:
# I directly create the variable inside __init__ and not outside __init__ and just assign a value in __init__
class Car:
def __init__(self, color):
self.color = color
有影响吗?
【问题讨论】:
标签: python python-3.x class initialization member