<div>Hi everybody,</div>
<div>I've been reading the chapter of classes&nbsp;of Byte of Python by Swaroop. There's an example with classes (11.4) that is below:</div>
<div>&nbsp;</div>
<div>#############################################</div>
<div><span class="py-statement">class</span> <span class="py-identifier">Person</span>:<br><span class="py-string">'''Represents a person.'''</span><br>population = <span class="py-number">0</span><br><br><span class="py-statement">
def</span> <span class="py-identifier">__init__</span>(<span class="py-builtin">self</span>, name):<br><span class="py-string">'''Initializes the person's data.'''</span><br><span class="py-builtin">self</span>.name = name
<br><span class="py-statement">print</span> <span class="py-string">'(Initializing %s)'</span> % <span class="py-builtin">self</span>.name<br><br><span class="py-comment"># When this person is created, he/she</span><br><span class="py-comment">
# adds to the population</span><br>Person.population += <span class="py-number">1</span><br><br><span class="py-statement">def</span> <span class="py-identifier">__del__</span>(<span class="py-builtin">self</span>):<br><span class="py-string">
'''I am dying.'''</span><br><span class="py-statement">print</span> <span class="py-string">'%s says bye.'</span> % <span class="py-builtin">self</span>.name<br><br>Person.population -= <span class="py-number">1</span><br>
<br><span class="py-statement">if</span> Person.population == <span class="py-number">0</span>:<br><span class="py-statement">print</span> <span class="py-string">'I am the last one.'</span><br><span class="py-statement">
else</span>:<br><span class="py-statement">print</span> <span class="py-string">'There are still %d people left.'</span> % Person.population<br><br><span class="py-statement">def</span> <span class="py-identifier">sayHi</span>
(<span class="py-builtin">self</span>):<br><span class="py-string">'''Greeting by the person.<br><br>Really, that's all it does.'''</span><br><span class="py-statement">print</span> <span class="py-string">'Hi, my name is %s.'
</span> % <span class="py-builtin">self</span>.name<br><br><span class="py-statement">def</span> <span class="py-identifier">howMany</span>(<span class="py-builtin">self</span>):<br><span class="py-string">'''Prints the current population.'''
</span><br><span class="py-statement">if</span> Person.population == <span class="py-number">1</span>:<br><span class="py-statement">print</span> <span class="py-string">'I am the only person here.'</span><br><span class="py-statement">
else</span>:<br><span class="py-statement">print</span> <span class="py-string">'We have %d persons here.'</span> % Person.population<br><br>swaroop = Person(<span class="py-string">'Swaroop'</span>)<br>swaroop.sayHi()<br>
swaroop.howMany()<br><br>kalam = Person(<span class="py-string">'Abdul Kalam'</span>)<br>kalam.sayHi()<br>kalam.howMany()<br><br>swaroop.sayHi()<br>swaroop.howMany()<br>&nbsp;</div>
<div>#################################</div>
<div>EXAMPLE'S OUTPUT (Output shown in the example)</div>
<div>&nbsp;</div>
<div>$ python objvar.py<br>(Initializing Swaroop)<br>Hi, my name is Swaroop.<br>I am the only person here.<br>(Initializing Abdul Kalam)<br>Hi, my name is Abdul Kalam.<br>We have 2 persons here.<br>Hi, my name is Swaroop.
<br>We have 2 persons here.<br>Abdul Kalam says bye.<br>There are still 1 people left.<br>Swaroop says bye.<br>I am the last one.</div>
<div>##############################</div>
<div>MY OUTPUT</div>
<div>&nbsp;</div>
<div>(Initializing Swaroop)<br>Hi, my name is Swaroop.<br>I am the only person here.<br>(Initializing Abdul Kalam)<br>Hi, my name is Abdul Kalam.<br>We have 2 persons here.<br>Hi, my name is Swaroop.<br>We have 2 persons here.
</div>
<div>##################################</div>
<div>&nbsp;</div>
<div>I have runned the script in both Linux and Windows and got the same result. Could you explain me what's wrong with this???</div>
<div><br>-- <br>Edgar A. Rodriguez</div>