//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. typeof(), __typeof(), __typeof__(), -isKindOfClass:的区别

    关于  typeof()和 __typeof()  和 __typeof__() ,Stack Overflow 有一段解释,大概意思是, __typeof() .__typeof__() 是C语言的 ...

  2. MVC 实用构架实战(一)——项目结构搭建

    一.前言 在<上篇>中,已经把项目整体结构规划做了个大概的规划.在本文中,将使用代码的方式来一一解说各个层次.由于要搭建一个基本完整的结构,可能文章会比较长.另外,本系列主要出于实用的目的 ...

  3. 2016年蓝桥杯省赛A组c++第1题

    /* 某君新认识一网友. 当问及年龄时,他的网友说: “我的年龄是个2位数,我比儿子大27岁, 如果把我的年龄的两位数字交换位置,刚好就是我儿子的年龄” 请你计算:网友的年龄一共有多少种可能情况? 提 ...

  4. [development][suricata] linux下一代权限控制 capabilities

    读suricata源码过程中,  读到了 libcap-ng 应该就是anthroid手机,每次安装app时候, 询问的那个capablities.....吧.... 中文文档: http://rk7 ...

  5. [web][nginx] 初识nginx -- 使用nginx搭建https DPI解码测试环境

    环境 CentOS 7 X86 文档: https://nginx.org/en/docs/ 安装: [root@dpdk ~]# cat /etc/yum.repos.d/nginx.repo [n ...

  6. redis设置bind

    1>注释掉bind #bind 127.0.0.1 2>默认不是守护进程方式运行,这里可以修改 daemonize no 3>禁用保护模式 protected-mode no 启动R ...

  7. Oracle内置函数SQLCODE和SQLERRM的使用

    在我们写proc程序中经常要有错误处理,在错误处理中我们经常要输出错误信息来给帮助我们分析和解决错误原因,从而更正数据.这时候就会用到SQLCODE和SQLERRM. SQLCode:数据库操作的返回 ...

  8. thinkphp模板使用

    1.模板文件 就是个html,可以保存到View的Public文件夹下,比如叫base.html(参考onethink) <block name="a">a</b ...

  9. MySQL模拟Oralce闪回操作

    在前面的文章中我们介绍了MySQL误操作后数据恢复(update,delete忘加where条件),大概操作是通过sed命令把binlog中相关SQL误操作给逆向回来,然后导入SQL文件来恢复错误操作 ...

  10. 6个laravel常用目录路径函数

    public_path() public_path函数返回public目录的绝对路径:$path = public_path(); base_path() base_path函数返回项目根目录的绝对路 ...