『jollychang专用问答贴』__del__问题

『jollychang专用问答贴』__del__问题

5楼有更新

没人答那我就再问一个
#!/usr/bin/python
# Filename: while.py

number = 23
running = True

while running:
    guess = int(raw_input('Enter an integer : '))
    if guess == number:
        print 'Congratulations, you guessed it.'
        print 'The while loop is over.'
        running = False # this causes the while loop to stop     
    elif guess < number:
        print 'No, it is a little higher than that'
    else:
        print 'No, it is a little lower than that'
#else:

    # Do anything else you want to do here
print 'Done'


QUOTE:
python D:\MyPython\while.py
Process started >>>
  File "D:\MyPython\while.py", line 11
    print 'The while loop is over.'
    ^
IndentationError: unexpected indent
<<< Process finished.
================ READY ================

为什么啊?
第一个问题:什么叫临时,什么叫长期存在,没说清楚
第二个问题:提示已经很明确,是缩近的问题。要看你的程序的缩近是什么,会不会是tab和空格混合了,或者没有对齐。


QUOTE:
原帖由 limodou 于 2008-3-17 20:57 发表
第一个问题:什么叫临时,什么叫长期存在,没说清楚
第二个问题:提示已经很明确,是缩近的问题。要看你的程序的缩近是什么,会不会是tab和空格混合了,或者没有对齐。

第二个问题,谢谢,确实是缩进的问题,虽然看起来一样,但是一个是tab,一个是space,多谢
第一个问题,代码在公司,我忘记了,我前面试了下,应该是我的问题,多谢

#!/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
        erson.population += 1

    def __del__(self):
        '''I am dying.'''
        print '%s says bye.' % self.name

        erson.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()
print '***************************'
kalam = Person('Abdul Kalam')
kalam.sayHi()
kalam.howMany()
print '***************************'
swaroop.sayHi()
swaroop.howMany()
print '***************************'

output:

QUOTE:
(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.
***************************
Hi, my name is Swaroop.
We have 2 persons here.
***************************
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
I am the last one.

继续是A_Byte_Of_Python里面的例子,我发贴前又一次百度了一下,确定没有答案,再来发的
我想问最后四行
如果说__del__是每次都自动运行
那为什么运行完__del__之后还要运行
sayHi()
howMany()?


QUOTE:
原帖由 jollychang 于 2008-3-31 16:09 发表
#!/usr/bin/python
# Filename: objvar.py

class Person:
    '''Represents a person.'''
    population = 0

    def __init__(self, ...

最后4行明明是脚本结束的时候触发类实例的__del__才产生出来的,哪里有什么sayHi,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)


我也在查这个程序的原因呢
结果查明了
应该是和os有关
在ubuntu6.06上测试通过
windows上还是不行