python class 2
//test.py
1 class Employee:
2 'all employee'
3 empCount = 0
4 def __init__(self, name, salary):
5 self.name = name
6 self.salary = salary
7 Employee.empCount += 1
8 def prt(self):
9 print 'self ', self
10 print '__class__', self.__class__
11 def displayCount(self):
12 print 'Total Employee %d' % Employee.empCount
13 def displayEmployee(self):
14 print 'Name: ', self.name, ', Salary: ', self.salary
15
16 ee = Employee('zcl', 0)
17 print 'doc', Employee.__doc__
18 ee.displayCount()
19 ee.displayEmployee()
20 ee.prt()
21 print 'hasattr', hasattr(ee, 'empCount'), ee.empCount
22 print 'getattr', getattr(ee, 'empCount')
23 print 'setattr', setattr(ee, 'empCount', 2), ee.empCount
24 print 'delattr', delattr(ee, 'empCount')
25 print 'hasattr', hasattr(ee, 'empCount')
26 print '__dict__', ee.__dict__
27 #print '__name__', ee.__name__
28 print '__module__', ee.__module__
29 #print '__bases__', ee.__bases__
30
31 class Name:
32 def __init__(self):
33 self.name = 'I am father'
34 def method(self):
35 print 'p', self.name
36 def vmethod(self):
37 print 'vp', 'I am parent method'
38
39 class Point(Name):
40 def __init__(self, x=0,y=0):
41 self.x = x
42 self.y = y
43 Name.__init__(self)
44 def __del__(self):
45 class_name = self.__class__.__name__
46 print class_name, 'destroied'
47 def submethod(self):
48 Name.method(self)
49 def vmethod(self):
50 print 'vp', 'I am child method'
51 def __repr__(self):
52 print '__repr__'
53 return 'repr finished'
54 def __cmp__(self, x):
55 if 1==x :
56 return 1
57 else:
58 return 2
59 def __add__(self, other):
60 return Point(self.x+other.x, self.y+other.y)
61 __privateData = "You can not see me, but properly can!"
62
63 pt1 = Point()
64 pt2 = pt1
65 pt3 = pt1
66
67 print id(pt1), id(pt2), id(pt3)
68 pt1.submethod()
69 pt1.vmethod()
70 print repr(pt1)
71 print cmp(pt1, 1)
72 del pt1
73 del pt2
74 del pt3
75 p3 = Point(1, 2)+Point(4, 5)
76 print 'p3.x, p3.y', p3.x, p3.y
77 print 'privateData ', p3._Point__privateData
//result
# python test.py
doc all employee
Total Employee 1
Name: zcl , Salary: 0
self <__main__.Employee instance at 0x7fa0a01c4200>
__class__ __main__.Employee
hasattr True 1
getattr 1
setattr None 2
delattr None
hasattr True
__dict__ {'salary': 0, 'name': 'zcl'}
__module__ __main__
140327857701664 140327857701664 140327857701664
p I am father
vp I am child method
__repr__
repr finished
1
Point destroied
Point destroied
Point destroied
p3.x, p3.y 5 7
privateData You can not see me, but properly can!
Point destroied
Finally:
python 支持多继承,这样一来,它就是c++的脚本版本了吧!!哈哈,但其它高级语言都在弱化多继承,在此,我也不多做讨论了
另外,多说一句,python里保护型成员用单下划线'_'
OK,就这么吧
python class 2的更多相关文章
- Python中的多进程与多线程(一)
一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
- Python 小而美的函数
python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况 any any(iterable) ...
- JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议
软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...
- 可爱的豆子——使用Beans思想让Python代码更易维护
title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...
- 使用Python保存屏幕截图(不使用PIL)
起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...
- Python编码记录
字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...
- Apache执行Python脚本
由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...
- python开发编译器
引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...
- 关于解决python线上问题的几种有效技术
工作后好久没上博客园了,虽然不是很忙,但也没学生时代闲了.今天上博客园,发现好多的文章都是年终总结,想想是不是自己也应该总结下,不过现在还没想好,等想好了再写吧.今天写写自己在工作后用到的技术干货,争 ...
随机推荐
- N!
求N! Problem Description Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! Input One N ...
- Linux 创建文件系统及挂载文件系统流程详解(转)
作者:北南南北 来自: LinuxSir.Org 摘要:本文对新增硬盘,切割硬盘,创建硬盘分区,为硬盘分区创建文件系统,以及加载文件系统的流程做总结性论述:主要是为初学者弄清楚这一操作过程:本文涉及f ...
- confd
1.下载安装confd ]# wget https://github.com/kelseyhightower/confd/releases/download/v0.15.0/confd-0.15.0- ...
- Page3:组合系统状态空间输入输出描述、矩阵指数函数性质[Linear System Theory]
内容包含组合系统的状态空间描述以及输入输出描述,零输入响应的概念以及矩阵指数函数的性质
- pause
https://stackoverflow.com/questions/37063700/mm-pause-usage-in-gcc-on-intel?utm_medium=organic&u ...
- [DPI][TCP] linux API的接口如何控制urgent包的收发
做DPI,写协议栈的时候,处理到了urgent数据包.突然好奇应用层是如何控制发出urgent包的呢?而接收端又是如何知道,接受到了urgent包的呢? man 7 tcp,中有如下一段: TCP s ...
- fastreport好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了
好象将想合并哪个单元就将那一列的TEXT控件的Merge的属性设成True就可以了 可以用FASTREPORT中的分组打印,你看一下里面的DEMO,里面都有的, 高版本的有suppressRepeat ...
- OLTP/OLAP
原文地址:https://www.cnblogs.com/hhandbibi/p/7118740.html 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction p ...
- 20165336 学习基础与C语言基础调查
20165336 技能学习心得与c语言学习 一.心得体会 做教练 从老师的健身教练健身学员的学习关系中我懂得了学生应该有自主的学习意识,要有计划地去训练.去流汗,并且要以100分的要求严于律己,老师是 ...
- 求助!使用 ReportViewer 控件集成 Reporting Services2008 时,报"...401 unauthorized"错误!
实现接口 public class ReportServiceCredetials : Microsoft.Reporting.WebForms.IReportServerCredentials { ...