sorry but stil dont see how it gets the value !
i understand each single line
from class variable or local variable
it is all very logical each var is a taking a specific value
no problems with that!
but in the case of the robot population and static method
i still don’t get how the howmany function is getting the value from robot .population
here is the code
import bpy
class Robot:
'''Represents a robot, with a name.'''
# A class variable, counting the number of robots
population = 0
def __init__(self, name):
'''Initializes the data.'''
self.name = name
print('(Initializing {0})'.format(self.name))
# When this person is created, the robot
# adds to the population
Robot.population += 1
def __del__(self):
'''I am dying.'''
print('{0} is being destroyed!'.format(self.name))
Robot.population -= 1
if Robot.population == 0:
print('{0} was the last one.'.format(self.name))
else:
print('There are still {0:d} robots working.'.format(Robot.population))
def sayHi(self):
'''Greeting by the robot.
''' Yeah, they can do that.'''
print('Greetings, my masters call me {0}.'.format(self.name))
def howMany():
'''Prints the current population.'''
print('We have {0:d} robots.'.format(Robot.population))
howMany = staticmethod(howMany)
droid1 = Robot('R2-D2')
droid1.sayHi()
Robot.howMany()
########
where does Robot.howMany() get the value and how !
i can see the print line
print(‘We have {0:d} robots.’.format(Robot.population))
is taking the Robot.population value but how does howmany function gets it ?
is it passed on and how
this “howmany” name is not a predefine function in python it could be anyname here
so it has to be given a value or calculalte it but how ?
i guess it’s getting i from this line
howMany = staticmethod(howMany)
but how does the staticmethod(howMany) get it ?
also in the book there is another way to define this
which are call decorator as per the book
@staticmethod
def howMany():
‘’‘Prints the current population.’’’
print(‘We have {0:d} robots.’.format(Robot.population))
but it does not explain the logic here
how does the robot.population is given it’s value to the function ?
theses decorator can do very strange things like shown in the link gien in book
but i’ll talk about theses later
http://www.ibm.com/developerworks/linux/library/l-cpdecor.html
Thanks