Python类的定义与使用
- #! /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类的定义与使用的更多相关文章
- python类的定义和使用
python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...
- python 类的定义 实例化 实例完后初始化
先来看看 类的__init__, 类的__new__ , 元类的__new__的执行顺序 class TMetaclass(type): def __new__(cls,name,bases,attr ...
- Python类的定义、方法和属性使用
类用来描述具有相同的属性和方法的对象的集合.对于在类中定义的函数,称为方法.类变量不直接叫做类变量,称为属性. 1.类的定义 例子: class User(): pass 说明: (1)定义了一个类名 ...
- Python类的定义
Python笔记--类定义 一.类定义: class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性 如果直接使用类名修改其属 ...
- python 类的定义和继承
python 2 中类 一.类定义: ? 1 2 class <类名>: <语句> 类实例化后,可以使用其属性,实际上,创建一个类之后,可以通过类名访问其属性如果直接使用类 ...
- python类及其方法
python类及其方法 一.介绍 在 Python 中,面向对象编程主要有两个主题,就是类和类实例类与实例:类与实例相互关联着:类是对象的定义,而实例是"真正的实物",它存放了类中 ...
- python类:magic魔术方法
http://blog.csdn.net/pipisorry/article/details/50708812 魔术方法是面向对象Python语言中的一切.它们是你可以自定义并添加"魔法&q ...
- (转)python类:magic魔术方法
原文:https://blog.csdn.net/pipisorry/article/details/50708812 版权声明:本文为博主皮皮http://blog.csdn.net/pipisor ...
- python类定义
在我的收藏中有一篇特别详细的类讲解 此处部分内容引自:http://blog.sina.com.cn/s/blog_59b6af690101bfem.html class myclass: 'this ...
随机推荐
- SQL 分组查询 group by
select * from emp --deptno 为部门号 --输出每个部门的编号 和 该部门的平均工资 --group by deptno; 通过deptno分组 select deptno, ...
- php类与对象简单操作
<?php /* * Created on 2015-8-25 * * To change the template for this generated file go to * Window ...
- C# WinForm动态添加MSChart控件
添加mschart.dll动态链接库 添加引用 System.Windows.Forms.DataVisualization MSChart控件作为方便的用户数据展示控件,可以方便的使用控件提 ...
- sql 存储过程参数是表类型,数据库中如何调用
DECLARE @NEW_STUDENT as [CancelLendersContent] INSERT @NEW_STUDENT VALUES (0,0,0,'12345678912','张三', ...
- -XX:+PrintHeapAtGC 每次一次GC后,都打印堆信息
-XX:+PrintHeapAtGC每次一次GC后,都打印堆信息 {Heap before GC invocations=0 (full 0): def new generation total ...
- 20道C#练习题(一)1——10题
1.输入三个整数,xyz,最终以从小到大的方式输出.利用if嵌套. Console.Write("请输入x="); double x = double.Parse(Console. ...
- Android多线程通信之Handler
主线程 public class MainActivity extends ActionBarActivity { private Handler handler; // private Thread ...
- Android 二维码扫描框 加四个角及中间横线自动下滑
红色为加四个角 黄色为扫描线自动下滑 /* * Copyright (C) 2008 ZXing authors * * Licensed under the Apache License, Ver ...
- python读入文件
举例说明吧,路径一定要带转义符‘\’,下面的例子中,每一行是一个样本的feature >>> myfile=open("D:\\301\\graduate_thesis\\ ...
- 虚拟机 本地 本机 双启动 运行 vhd local Dual Boot
在使用虚拟机的过程中, 可能会遇到虚拟机的运行要求过高, 电脑力不从心的情况. 为了让虚拟机使用更多电脑资源, 可以让虚拟机以本地双系统的方式,访问本地计算机资源. 打开磁盘管理,在磁盘上右键,选择 ...