摘抄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 .name if __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 .name if __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 .name if __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 ." ...
随机推荐
- DevExpress ASP.NET 使用经验谈(6)-ASPxGridView属性设置与CRUD界面优化
上一节中,我们通过简单的配置,通过ASPxGridView控件的使用,完成了对数据库表的CRUD操作. 这样的界面展现,功能是达到了,但是操作体验上,还是有所欠缺的. 图一 默认生成的列表界面 图二 ...
- iOS开发之第三方登录微信-- 史上最全最新第三方登录微信方式实现
项目地址 : https://github.com/zhonggaorong/weixinLoginDemo 最新版本的微信登录实现步骤实现: 1.在进行微信OAuth2.0授权登录接入之前,在 ...
- LeetCode 二叉树的最小深度
计算二叉树的最小深度.最小深度定义为从root到叶子节点的最小路径. public class Solution { public int run(TreeNode root) { if(root = ...
- linux下出现java.net.UnknownHostException
项目部署在win环境下没问题,但是在迁移生产环境的时候出现Java.net.UnknowHostException 原因在于etc/hosts 文件没有配置域名映射,使用vi编辑器加上服务器ip 以及 ...
- myEclipse快捷键及其常用设置
快捷键: 查找替换:ctrl + f 复制行: ctrl + alt + down 删除行: ctrl + d 插入行: shift + enter, ctrl + shift ...
- Oracle/Mysql批量插入的sql,效率比较高
1.oracle 批量插入: insert into tableName(col1,col2,col3...) select 1,'第一行第一列值','第二列值' from dual union ...
- poj 1905 Expanding Rods 二分
/** 题解晚上写 **/ #include <iostream> #include <math.h> #include <algorithm> #include ...
- git 代理设置
git 代理设置: git config --global http.proxy http://proxy.com:8080git config --global https.proxy http:/ ...
- java学习之xml
xml的处理有两种方式dom和Sax 其中dom有3套api ,分别是dom和jdom和dom4j package com.gh.xml; import java.io.File; import ja ...
- BZOJ 1015
program bzoj1015; {$inline on} ; type node=record togo,next:longint; end; var tot,n,m,d,cnt:longint; ...