__init__ 为什么python类需要初始化

Python09

__init__ 为什么python类需要初始化,第1张

通常构造一个类,用 init 初始化

为什么python类需要初始化

1.python 类初始化属性 def __init__() 中定义,实例化对象后可直接调用

2.python 类非初始化属性在 def func() 中定义,实例化对象后, 先调用调用函数,再调用属性

结果:

Python中,tuple和list均为内置类型, 以list作为参数将tuple类初始化,将返回tuple类型 tuple([1,2,3]) #list转换为tuple以tuple作为参数将list类初始化,将返回list类型 list((1,2,3)) #tuple转换为list