初学python ,看简明python教程,有一点不明白,这是教程中的一段代码,红色是我添加的,不太明白为什么会这样,谁能讲一下.书中说:"类的变量 由一个类的所有对象(实例)共享使用。只有一个类变量的拷贝,所以当某个对象对类的变量做了改动的时候,这个改动会反映到所有其他的实例上。",可我修改了swaroop.population ,为什么不影响呢howmany()呢,显示还是原来的?
#!/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 '(Initializing %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
Person.population -= 1
if Person.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
swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
swaroop.population +=5
print swaroop.population
swaroop.sayHi()
swaroop.howMany()
print swaroop.population
执行结果:
(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
7
Hi, my name is Swaroop.
We have 2 persons here.
7
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
I am the last one.
初学python,类和对象有点不明白,请教了.
-
- 帖子: 1
- 注册时间: 2007-11-29 19:47
- nobrain
- 帖子: 808
- 注册时间: 2005-08-25 13:58
- 来自: ustc
- 联系:
-
- 帖子: 140
- 注册时间: 2008-03-26 12:19
- 联系:
- woaiwojia
- 帖子: 1355
- 注册时间: 2007-09-10 20:20
- 系统: Debian
- 来自: 南京
-
- 帖子: 6
- 注册时间: 2008-09-19 23:55
Re: 初学python,类和对象有点不明白,请教了.
对象是改不了类变量的值的!改的只是属于它的,所以就是你看到的那种结果lylcwj 写了:初学python ,看简明python教程,有一点不明白,这是教程中的一段代码,红色是我添加的,不太明白为什么会这样,谁能讲一下.书中说:"类的变量 由一个类的所有对象(实例)共享使用。只有一个类变量的拷贝,所以当某个对象对类的变量做了改动的时候,这个改动会反映到所有其他的实例上。",可我修改了swaroop.population ,为什么不影响呢howmany()呢,显示还是原来的?
#!/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 '(Initializing %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
Person.population -= 1
if Person.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
swaroop = Person('Swaroop')
swaroop.sayHi()
swaroop.howMany()
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
swaroop.population +=5
print swaroop.population
swaroop.sayHi()
swaroop.howMany()
print swaroop.population
执行结果:
(Initializing Swaroop)
Hi, my name is Swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
7
Hi, my name is Swaroop.
We have 2 persons here.
7
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
I am the last one.
- jarlyyn
- 帖子: 4671
- 注册时间: 2006-04-12 18:54
- 联系:
Re: 初学python,类和对象有点不明白,请教了.
没有缩进的python代码更本不是人看得-_____-
找了下原文。
楼主看看原文中是怎么用的
这里面的Person是什么呢?是类的名字吧?。就是说, Person[object]这个东西的pupulation属性+1。
然后,让我们定义一个 叫Tom的Person对象.
然后,让我们看看tom和Person的关系。
很明显,tom是 Person这个函数(其实是Person类的构建函数)的一个返回值(我们称之为Person__init__)。
Tom[Person__init__]很明显和 Person[object]没有直接关系。
原始代码里是设置Person 这个object的popultation的值
而你设置的是Tom这个 Person__init__值,当然没有直接联系了,都不是一个层次的东西啊。
最后,关于你代码的正确写法,应该是
找了下原文。
楼主看看原文中是怎么用的
代码: 全选
Person.population += 1
然后,让我们定义一个 叫Tom的Person对象.
代码: 全选
tom=Person()
很明显,tom是 Person这个函数(其实是Person类的构建函数)的一个返回值(我们称之为Person__init__)。
Tom[Person__init__]很明显和 Person[object]没有直接关系。
原始代码里是设置Person 这个object的popultation的值
而你设置的是Tom这个 Person__init__值,当然没有直接联系了,都不是一个层次的东西啊。
最后,关于你代码的正确写法,应该是
代码: 全选
Person.population +=5