1. #! /usr/bin/python
  2. # Filename:objvar.py
  3.  
  4. class Person:
  5. '''Represents a person.'''
  6. population = 0
  7.  
  8. def __init__(self, name):
  9. ''' Initializes the person's data. '''
  10. self.name = name
  11. print '(Initializeing %s)' % self.name
  12.  
  13. # When this person is created, he/she adds to the population
  14. Person.population += 1
  15.  
  16. def __del__(self):
  17. ''' I am dying. '''
  18. print '%s says bye.' % self.name
  19.  
  20. self.__class__.population -= 1
  21.  
  22. if self.__class__.population == 0:
  23. print 'I am the last one.'
  24. else:
  25. print 'There are still %d people left.' % Person.population
  26.  
  27. def sayHi(self):
  28. ''' Greeting by the person.
  29. Really, that's all it does. '''
  30. print 'Hi, my name is %s.' % self.name
  31.  
  32. def howMany(self):
  33. ''' Prints the current population. '''
  34. if Person.population == 1:
  35. print 'I am the only person here.'
  36. else:
  37. print 'We have %d persons here.' % Person.population
  38.  
  39. # The definion of class is end
  40.  
  41. zjw = Person('zjw')
  42. zjw.sayHi()
  43. zjw.howMany()
  44.  
  45. lbz = Person('lbz')
  46. lbz.sayHi()
  47. lbz.howMany()
  48.  
  49. zjw.sayHi()
  50. zjw.howMany()

  程序刚开始是有错误的,就是在__del__函数中如果使用Person.population来调用全局变量的话,会出现下面这个错误

Exception AttributeError: "'NoneType' object has no attribute 'population'" in <bound method Person.__del__ of <__main__.Person instance at 0xb72e9aec>> ignored

在网上搜了一下,应该是说在del函数中不能访问全局变量,因此就修改成了现在这个这样,使用对象的一个方法间接的访问到了全局变量。

发现现在网上到处都是转载的文章,很少用真正原创的,我想说的是,就算是转载的,也请你看懂了之后再转,否则还是半知半解,只是在网络上图增垃圾而已。这个问题我还没搞明白到底的原因,如果哪位知道是什么原因,请留言告诉我,谢谢。

Python类的定义与使用的更多相关文章

  1. python类的定义和使用

    python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...

  2. python 类的定义 实例化 实例完后初始化

    先来看看 类的__init__, 类的__new__ , 元类的__new__的执行顺序 class TMetaclass(type): def __new__(cls,name,bases,attr ...

  3. Python类的定义、方法和属性使用

    类用来描述具有相同的属性和方法的对象的集合.对于在类中定义的函数,称为方法.类变量不直接叫做类变量,称为属性. 1.类的定义 例子: class User(): pass 说明: (1)定义了一个类名 ...

  4. Python类的定义

    Python笔记--类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...

  5. python 类的定义和继承

    python 2 中类 一.类定义: ? 1 2 class <类名>:   <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性如果直接使用类 ...

  6. python类及其方法

    python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...

  7. python类:magic魔术方法

    http://blog.csdn.net/pipisorry/article/details/50708812 魔术方法是面向对象Python语言中的一切.它们是你可以自定义并添加"魔法&q ...

  8. (转)python类:magic魔术方法

    原文:https://blog.csdn.net/pipisorry/article/details/50708812 版权声明:本文为博主皮皮http://blog.csdn.net/pipisor ...

  9. python类定义

    在我的收藏中有一篇特别详细的类讲解 此处部分内容引自:http://blog.sina.com.cn/s/blog_59b6af690101bfem.html class myclass: 'this ...

随机推荐

  1. SQL 分组查询 group by

    select * from emp --deptno 为部门号 --输出每个部门的编号 和 该部门的平均工资 --group by deptno; 通过deptno分组 select deptno, ...

  2. php类与对象简单操作

    <?php /* * Created on 2015-8-25 * * To change the template for this generated file go to * Window ...

  3. C# WinForm动态添加MSChart控件

    添加mschart.dll动态链接库 添加引用 System.Windows.Forms.DataVisualization     MSChart控件作为方便的用户数据展示控件,可以方便的使用控件提 ...

  4. sql 存储过程参数是表类型,数据库中如何调用

    DECLARE @NEW_STUDENT as [CancelLendersContent] INSERT @NEW_STUDENT VALUES (0,0,0,'12345678912','张三', ...

  5. -XX:+PrintHeapAtGC 每次一次GC后,都打印堆信息

    -XX:+PrintHeapAtGC每次一次GC后,都打印堆信息 {Heap before GC invocations=0 (full 0): def new generation   total ...

  6. 20道C#练习题(一)1——10题

    1.输入三个整数,xyz,最终以从小到大的方式输出.利用if嵌套. Console.Write("请输入x="); double x = double.Parse(Console. ...

  7. Android多线程通信之Handler

    主线程 public class MainActivity extends ActionBarActivity { private Handler handler; // private Thread ...

  8. Android 二维码扫描框 加四个角及中间横线自动下滑

    红色为加四个角  黄色为扫描线自动下滑 /* * Copyright (C) 2008 ZXing authors * * Licensed under the Apache License, Ver ...

  9. python读入文件

    举例说明吧,路径一定要带转义符‘\’,下面的例子中,每一行是一个样本的feature >>> myfile=open("D:\\301\\graduate_thesis\\ ...

  10. 虚拟机 本地 本机 双启动 运行 vhd local Dual Boot

    在使用虚拟机的过程中, 可能会遇到虚拟机的运行要求过高, 电脑力不从心的情况. 为了让虚拟机使用更多电脑资源, 可以让虚拟机以本地双系统的方式,访问本地计算机资源. 打开磁盘管理,在磁盘上右键,选择 ...