摘抄python __init__
注意1、__init__并不相当于C#中的构造函数,执行它的时候,实例已构造出来了。
|
1
2
3
4
5
|
class A(object): def __init__(self,name): self.name=name def getName(self): return 'A '+self.name |
当我们执行
|
1
|
a=A('hello') |
时,可以理解为
|
1
2
|
a=object.__new__(A)A.__init__(a,'hello') |
即__init__作用是初始化已实例化后的对象。
注意2、子类可以不重写__init__,实例化子类时,会自动调用超类中已定义的__init__
|
1
2
3
4
5
6
7
|
class B(A): def getName(self): return 'B '+self.nameif __name__=='__main__': b=B('hello') print b.getName() |
但如果重写了__init__,实例化子类时,则不会隐式的再去调用超类中已定义的__init__
|
1
2
3
4
5
6
7
8
9
|
class C(A): def __init__(self): pass def getName(self): return 'C '+self.nameif __name__=='__main__': c=C() print c.getName() |
则会报"AttributeError: 'C' object has no attribute 'name'”错误,所以如果重写了__init__,为了能使用或扩展超类中的行为,最好显式的调用超类的__init__方法
|
1
2
3
4
5
6
7
8
9
|
class C(A): def __init__(self,name): super(C,self).__init__(name) def getName(self): return 'C '+self.nameif __name__=='__main__': c=C('hello') print c.getName() |
摘抄python __init__的更多相关文章
- python __init__.py用途
转自http://www.cnpythoner.com/post/2.html Python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时, ...
- python __init__.py
python中的Module是比较重要的概念.常见的情况是,事先写好一个.py文 件,在另一个文件中需要import时,将事先写好的.py文件拷贝 到当前目录,或者是在sys.path中增加事先写好的 ...
- 高产的母猪之 python __init__全解
python __init__.py python 识别是不是一个模块的标准是目录下有无 __init__.py 模糊导入 模糊导入中的*中的模块是由__all__来定义的,__init__.py的 ...
- Python __init__.py 作用详解
__init__.py 文件的作用是将文件夹变为一个Python模块,Python 中的每个模块的包中,都有__init__.py 文件. 通常__init__.py 文件为空,但是我们还可以为它增加 ...
- Python __init__.py文件的作用
我们经常在python的模块目录中会看到 "__init__.py" 这个文件,那么它到底有什么作用呢? 1. 模块包(module package)标识 如果你是使用pytho ...
- python __init__.py 的作用
__init__.py的主要作用是: . Python中package的标识,不能删除 . 定义__all__用来模糊导入 . 编写Python代码(不建议在__init__中写python模块,可以 ...
- Python __init__.py 文件使用
__init__.py的主要作用是: 1. Python中package的标识,不能删除 2. 定义__all__用来模糊导入 3. 编写Python代码(不建议在__init__中写python模块 ...
- Python __init__ 特殊方法
在Python中有很多以双下划线开头且以双下划线结尾的固定方法.他们会在特定的时机被触发执行. __init__ 就是其中之一,它会在实例化之后自动被调用.以完成实例的初始化. >>> ...
- Python __init__函数的使用
class Cat: def __init__(self,_name): self.name = _name def eat(self): print("i am eating ." ...
随机推荐
- block 解析 - 局部变量
局部变量 block内使用局部变量,一般都是截获变量(只读),截获离block初始化最近的一次的值. 引用官方文档: Stack (non-static) variables local to the ...
- Android JNI入门第三篇——jni头文件分析
一. 首先写了java文件: public class HeaderFile { private native void doVoid(); native int doShort(); native ...
- HDU 1711 Number Sequence KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1711 AC代码: #include <iostream> #include <cs ...
- 转载 js判断浏览器
$scope.identifyBrowser= function () { var userAgent = navigator.userAgent, rMsie = /(msie\s|trident. ...
- js和jQuery写简单下拉菜单
1.jQuery写法 <head> <meta http-equiv="Content-Type" content="text/html; charse ...
- Map 的遍历
一.Map的遍历 在后面java的开发过程中会遇到Map类的使用,然而map的遍历是一大问题. Map遍历用两种比较交代的方法: package edu.map; import java.util.H ...
- 「OC」 封装
一.面向对象和封装 面向对象的三大特性:封装.继承和多态 在OC语言中,使用@interface和@implementation来处理类. @interface就好像暴露在外面的时钟表面,像外界提 ...
- [C/C++基础]读写文件
1.打开.关闭文件: FILE* fp = fopen(string.c_str(), FLAG); string.c_str():需用C语言字符串形式: FLAG说明: r: 只读方式打开: w: ...
- 用Response对象的write方法和<%%>及<%=%>输出同样效果的乘法表格
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Response1.aspx ...
- java学习之IO对象流
//注意对象类要打标记实现Serializable接口 package com.gh; import java.io.FileInputStream; import java.io.FileNotFo ...