python中的类
来源:互联网 发布:flash cs6 mac 破解版 编辑:程序博客网 时间:2024/05/21 08:59
<
divnotes
Table of Contents
- 1 classes and objects
- 1.1 类成员和对象成员
- 1.2 特殊函数
- 1.3 私有函数
1 classes and objects
1.1 类成员和对象成员
类成员相当于C++中的static成员变量,实例成员是和C++中的成员变量对应的,如下是类 成员:
>>> class counter:... count = 0 1... def __init__(self):... self.__class__.count += 1 2... >>> counter<class __main__.counter at 010EAECC>>>> counter.count 30>>> c = counter()>>> c.count 41>>> counter.count1>>> d = counter() 5>>> d.count2>>> c.count2>>> counter.count2
1.2 特殊函数
函数名以两个下划线开始和结束的函数名为特殊函数 如_setitem_和_getitem_是和[]操作相关联的
class MyDict: def __init__(self): self.data = {} def __setitem__(self, k, v): self.data[k] = v print('__setitem__(',k,',',v,')') def __getitem__(self, k): print('__getitem__(',k,')=',self.data.get(k,0)) return self.data.get(k,0)d = MyDict()d[1] = 2print(d[1])
1.3 私有函数
函数名以两个下划线开始且不是两个下划线结束的函数 变量名同样遵循次规则
class PrivateFunc: def __init__(self): self.ary = [1,2,3] def __listAry(self): for i in self.ary: print(i)pf = PrivateFunc()try: pf.__listAry()except AttributeError as err: print('func not exist: ', err)
从程序看出,python其实是把函数名和变量名改名了,改名后的私有成员同样是public的, 可以直接调用
Date: 2013-04-16 16:39:51 中国标准时间
- 转:Python中的类
- python中的类
- python中的嵌套类
- python中的类
- python中的类
- python类中的方法
- python中的元类
- python中的元类
- python中的类
- Python中的元类
- python中的类编程
- Python中的类
- Python中的元类
- Python 中的类
- python中的类class
- Python中的类class
- Python中的类
- Python中的类
- EAS BOS生成EAS需要的id
- split 例子
- IE6 png 透明 (三种解决方法)
- certificate
- Win 7为防火墙添加一条规则
- python中的类
- android局域网通信(udp,tcp等)
- linux私房菜(基础篇2)
- 什么是函数嵌套和递归
- 利用ffmpeg来进行视频解码的完整示例代码(H.264)
- c++中char*\wchar_t*\string\wstring之间的相互转换
- 使用PinYin4j.jar将汉字转换为拼音
- PP 配置库位参与MRP运算 及物料主数据的相关设置
- 提高PHP效率的50点KnowHow