Python的Object基类的__init__方法
来源:互联网 发布:linux开机启动 编辑:程序博客网 时间:2024/06/07 09:02
Python的Object基类的__init__方法
Python的__init__方法是用来初始化的实例的,是在__new__()方法执行后但是还没有return的情况下执行的__init__()方法。__new__()方法是用来创建实例的。这个__init__类似于构造方法,是可以直接从object类中继承的。如果一个BaseClass有__init__()方法,那么他的派生类(子类)需要显示的执行这个方法,不然是无法初始化BaseClass类的属性。__init__()方法是没有返回值的,并且如果有除了self意外的参数的话,在调用时是需要指定的。
创建类:
class Mytest1:
def __init__(self,value):
self._value = value
self._children = []
def myprint(self):
print('You point value : ',self._value)
if __name__ == '__main__':
t = Mytest1(1)
t.myprint()
输出:
You point value : 1
如果此时在实例化Mytest1时没有将value这个参数指定值,那么就会报错:
Traceback (most recent call last):
File "E:\workspace\mystudy\com\dgb\object\myinit.py", line 17, in <module>
t = Mytest1()
TypeError: __init__() missing 1 required positional argument: 'value'
在父类和子类的环境中:
class Mytest1:
def __init__(self,value):
self._value = value
self._children = []
def myprint(self):
print('You point value : ',self._value)
class Mysubtest1(Mytest1):
def __init__(self,value,value1):
Mytest1.__init__(self,value)
self._value1 = value1
def myprint1(self):
print('sub class _value1 : ',self._value1)
print('base class _value : ',self._value)
if __name__ == '__main__':
t = Mytest1(1)
t.myprint()
ts = Mysubtest1(1,10)
ts.myprint1()
如果在Mysubtest1中init方法中没有调用父类的init方法的情况下是无法访问Mytest1中的_value属性的:
Traceback (most recent call last):
File "E:\workspace\mystudy\com\dgb\object\myinit.py", line 30, in <module>
ts.myprint1()
File "E:\workspace\mystudy\com\dgb\object\myinit.py", line 23, in myprint1
print('base class _value : ',self._value)
AttributeError: 'Mysubtest1' object has no attribute '_value'
Python3.4版本中文档:
Called after the instance has been created (by __new__()), but before it is returned to the caller. The arguments are those passed to the class constructor expression. If a base class has an__init__() method, the derived class’s__init__() method, if any, must explicitly call it to ensure proper initialization of the base class part of the instance; for example: BaseClass.__init__(self,[args...]).
Because __new__() and __init__() work together in constructing objects (__new__() to create it, and __init__() to customise it), no non-None value may be returned by__init__(); doing so will cause aTypeError to be raised at runtime
开始系统的学习Python,从底层开始,夯实基础啊。一点一点积累。
- Python的Object基类的__init__方法
- Python---类的"__new__"和"__init__"方法
- python重载父类的__init__方法
- Python类的专用方法:__init__ | __del__等
- python子类如何调用父类的__init__方法
- python 构造器方法__init__的用法
- Python中__init__的方法解惑
- Python class的__new__与__init__方法
- python的__init__()
- python的 __init__ 理解
- python的__init__()
- python的__init__.py
- Python中的__init__方法与__new__方法的区别
- python中__init__()方法和__new__()方法的区别
- Python类里的__init__方法函数,Python类的构造函数
- 调用超类的__init__方法
- Python中新式类,子类使用super方法显式调用父类的__init__方法
- Python : 定义一个应用程序的初始化方法 __init__()
- Hibernate(一)HQL查询、原生sql查询
- 雾都的迷人
- Hibernate(二)HQL&SQL参数绑定、投影和分页以及命名查询
- 整理浏览器对网页的兼容性问题(一)——JS&DOM篇
- 细数site、收录、索引哪些误区
- Python的Object基类的__init__方法
- webrtc 视频通话质量的评价方法
- Spring定时任务的几种实现
- Linux进程间通信::命名管道
- 车牌归属地数据信息sql
- Python中的函数(二)
- 【Java多线程】ReentrantLock与Condition
- Hibernate(三)Criteria查询
- Linux下的守护进程