#! /usr/bin/python
# Filename:objvar.py class Person:
'''Represents a person.'''
population = 0 def __init__(self, name):
''' Initializes the person's data. '''
self.name = name
print '(Initializeing %s)' % self.name # When this person is created, he/she adds to the population
Person.population += 1 def __del__(self):
''' I am dying. '''
print '%s says bye.' % self.name self.__class__.population -= 1 if self.__class__.population == 0:
print 'I am the last one.'
else:
print 'There are still %d people left.' % Person.population def sayHi(self):
''' Greeting by the person.
Really, that's all it does. '''
print 'Hi, my name is %s.' % self.name def howMany(self):
''' Prints the current population. '''
if Person.population == 1:
print 'I am the only person here.'
else:
print 'We have %d persons here.' % Person.population # The definion of class is end zjw = Person('zjw')
zjw.sayHi()
zjw.howMany() lbz = Person('lbz')
lbz.sayHi()
lbz.howMany() zjw.sayHi()
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. UEditor和CKEditor配置上传图片,视频,附件

    UEditor: http://blog.sina.com.cn/s/blog_8bb128230102v12x.html CKEditor:http://blog.sina.com.cn/s/blo ...

  2. Leetcode: Shuffle an Array

    Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...

  3. 设置 BitmapData 没填充部分为透明色

    默认 BitmapData 是以白色填充的 在初始化时,设定上每3,4个参数即可透明: new BitmapData(w,h,true,0) 第4个参数要为0

  4. .NET: WPF DependencyProperty

    DependencyProperty and DependencyObject is the core of WPF data binding. We can use this two class t ...

  5. linux 压缩文件的命令总结

    Linux压缩文件的读取 *.Z       compress 程序压缩的档案: *.bz2     bzip2 程序压缩的档案: *.gz      gzip 程序压缩的档案: *.tar     ...

  6. ORA-000845 与 /dev/shm(tempfs)

    MEMORY_TARGET参数在Oracle 11g被引进,主要是用于控制Oracle对于系统内存的使用,首次将SGA与PGA整合到一起实现自动管理.一旦设置了MEMORY_TARGET参数值,Ora ...

  7. 请求在Struts2框架中的处理步骤

    上图来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2 ...

  8. Tomcat增加缓存

  9. linux性能分析工具

    概览 uptime dmesg | tail vmstat 1 mpstat -P ALL 1 pidstat 1 iostat -xz 1 free -m sar -n DEV 1 sar -n T ...

  10. iOS 学习笔记 十三 (2015.04.15)采用第三方库,实现ios录音转为amr

    1.第三方开源库地址 https://github.com/guange2015/ios-amr 2.参考博客地址 http://blog.csdn.net/windsoul85/article/de ...