初学python,类和对象有点不明白,请教了.

软件和网站开发以及相关技术探讨
回复
lylcwj
帖子: 1
注册时间: 2007-11-29 19:47

初学python,类和对象有点不明白,请教了.

#1

帖子 lylcwj » 2008-02-28 9:41

初学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.
shhcy2
帖子: 7
注册时间: 2007-05-11 16:11

#2

帖子 shhcy2 » 2008-03-03 9:14

没有缩进看起来真痛苦
头像
nobrain
帖子: 808
注册时间: 2005-08-25 13:58
来自: ustc
联系:

#3

帖子 nobrain » 2008-03-03 12:48

应该用 Person.population +=5
爱喝真猪奶茶的夜鸣猪
skyrose
帖子: 140
注册时间: 2008-03-26 12:19
联系:

#4

帖子 skyrose » 2008-03-28 1:47

shhcy2 写了:没有缩进看起来真痛苦
是啊,看惯了缩进,看这些有些累.
头像
woaiwojia
帖子: 1355
注册时间: 2007-09-10 20:20
系统: Debian
来自: 南京

#5

帖子 woaiwojia » 2008-04-02 21:51

skyrose 写了:
shhcy2 写了:没有缩进看起来真痛苦
是啊,看惯了缩进,看这些有些累.
createboy
帖子: 6
注册时间: 2008-09-19 23:55

Re: 初学python,类和对象有点不明白,请教了.

#6

帖子 createboy » 2008-09-20 2:27

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,类和对象有点不明白,请教了.

#7

帖子 jarlyyn » 2008-09-23 13:01

没有缩进的python代码更本不是人看得-_____-

找了下原文。

楼主看看原文中是怎么用的

代码: 全选

        Person.population += 1
这里面的Person是什么呢?是类的名字吧?。就是说, Person[object]这个东西的pupulation属性+1。

然后,让我们定义一个 叫Tom的Person对象.

代码: 全选

       tom=Person()
然后,让我们看看tom和Person的关系。

很明显,tom是 Person这个函数(其实是Person类的构建函数)的一个返回值(我们称之为Person__init__)。

Tom[Person__init__]很明显和 Person[object]没有直接关系。

原始代码里是设置Person 这个object的popultation的值

而你设置的是Tom这个 Person__init__值,当然没有直接联系了,都不是一个层次的东西啊。





最后,关于你代码的正确写法,应该是

代码: 全选

      Person.population +=5
回复