//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的更多相关文章

  1. Python中的多进程与多线程(一)

    一.背景 最近在Azkaban的测试工作中,需要在测试环境下模拟线上的调度场景进行稳定性测试.故而重操python旧业,通过python编写脚本来构造类似线上的调度场景.在脚本编写过程中,碰到这样一个 ...

  2. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

  3. Python 小而美的函数

    python提供了一些有趣且实用的函数,如any all zip,这些函数能够大幅简化我们得代码,可以更优雅的处理可迭代的对象,同时使用的时候也得注意一些情况   any any(iterable) ...

  4. JavaScript之父Brendan Eich,Clojure 创建者Rich Hickey,Python创建者Van Rossum等编程大牛对程序员的职业建议

    软件开发是现时很火的职业.据美国劳动局发布的一项统计数据显示,从2014年至2024年,美国就业市场对开发人员的需求量将增长17%,而这个增长率比起所有职业的平均需求量高出了7%.很多人年轻人会选择编 ...

  5. 可爱的豆子——使用Beans思想让Python代码更易维护

    title: 可爱的豆子--使用Beans思想让Python代码更易维护 toc: false comments: true date: 2016-06-19 21:43:33 tags: [Pyth ...

  6. 使用Python保存屏幕截图(不使用PIL)

    起因 在极客学院讲授<使用Python编写远程控制程序>的课程中,涉及到查看被控制电脑屏幕截图的功能. 如果使用PIL,这个需求只需要三行代码: from PIL import Image ...

  7. Python编码记录

    字节流和字符串 当使用Python定义一个字符串时,实际会存储一个字节串: "abc"--[97][98][99] python2.x默认会把所有的字符串当做ASCII码来对待,但 ...

  8. Apache执行Python脚本

    由于经常需要到服务器上执行些命令,有些命令懒得敲,就准备写点脚本直接浏览器调用就好了,比如这样: 因为线上有现成的Apache,就直接放它里面了,当然访问安全要设置,我似乎别的随笔里写了安全问题,这里 ...

  9. python开发编译器

    引言 最近刚刚用python写完了一个解析protobuf文件的简单编译器,深感ply实现词法分析和语法分析的简洁方便.乘着余热未过,头脑清醒,记下一点总结和心得,方便各位pythoner参考使用. ...

  10. 关于解决python线上问题的几种有效技术

    工作后好久没上博客园了,虽然不是很忙,但也没学生时代闲了.今天上博客园,发现好多的文章都是年终总结,想想是不是自己也应该总结下,不过现在还没想好,等想好了再写吧.今天写写自己在工作后用到的技术干货,争 ...

随机推荐

  1. PAT甲级1075 PAT Judge

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805393241260032 题意: 有m次OJ提交记录,总共有k道 ...

  2. .NET Core开发日志——Edge.js

    最近在项目中遇到这样的需求:要将旧有系统的一部分业务逻辑集成到新的自动化流程工具中.这套正在开发的自动化工具使用的是C#语言,而旧有系统的业务逻辑则是使用AngularJS在前端构建而成.所以最初的考 ...

  3. 泡泡一分钟:Exploiting Points and Lines in Regression Forests for RGB-D Camera Relocalization

    Exploiting Points and Lines in Regression Forests for RGB-D Camera Relocalization 利用回归森林中的点和线进行RGB-D ...

  4. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  5. Mybatis 不同使用方式

    前言 工作这么多年,ORM框架一直选择Mybatis框架. Mybatis的使用方式也一直在变,总体来说是越来越简单.写篇文章对各使用方式做个总结... 正文 一.Mybatis典型用法 1. 正常执 ...

  6. hyperledge vagrant docker development environment

    https://blog.csdn.net/zgljl2012/article/details/52896372

  7. Web开发——CSS基础

    参考: 参考:http://css.doyoe.com/ 参考:http://www.w3school.com.cn/cssref/index.asp 参考:https://www.w3cschool ...

  8. [dpdk] service core

    dpdk 17.11 增加了一组新的API,serivce core 如命名,就是用一组core跑service函数. 我自己的测试程序如下: https://github.com/tony-caot ...

  9. 内部排序->交换排序->快速排序

    文字描述  快速排序是对起泡排序的一种改进.它的基本思想是,通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分记录的关键字小,则可分别对这两部分记录继续进行排序,以达到整个 ...

  10. Linux and Oracle常用目录详解

    目录详解 目录 内容 / 根目录,一切从这里开始 /bin 包含系统启动和运行所必需的二进制文件(程序) /boot 包含Linux内核.最初的RAM磁盘映像(系统启动时,驱动程序会用到),以及启动加 ...