[Tutor] A question about the self and other stuff

Khalid Al-Ghamdi emailkgnow at gmail.com
Mon Oct 26 02:06:00 CET 2009


Hi everybody,
So I'm new to python and have questions about the following code:

class Robot:
    '''Represents a Robot with a name.

Deletes and makes Robots for testing perpuses'''

#a var for counting the number of Robots
    population = 0

    def __init__(self, name):
        '''initializes the data'''
        self.name=name
        print ('initializing {0}'.format(self.name))

        Robot.population+=1

    def __del__(self):
        '''I'm 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):
        '''This is just a hi thing.

    Just to say hi<<< to practice convention of docstrings'''

        print ('hello, khalid calls me {0}'.format(self.name))

    def howMany():
        '''prints how many'''

        print ('we have {0:d} Robots'.format (Robot.population))

    howMany= staticmethod(howMany)

droid1 = Robot('D23')
droid1.Sayhi()
Robot.howMany()

droid2 = Robot('F0009')
droid2.Sayhi()
Robot.howMany()

print ("\nRobots can do some work here\n")
print ("Robots have finished their work. So let's destroy them")

del droid1
del droid2
Robot.howMany()


1- In the class definition above, I don't get the "self.name=name". I
understand that the self is supposed to refer to the actual object, but
since it is an initialization method, there is no way to enter a name in
place of the arguments.
2- in the final few lines where I assign an object to the class, I notice
that a parameter was entered in the class name, "Robot(D23)", although when
defining the class I didn't put any arguments for it.
3- what is the difference between all the underscore, underscore attributes
like:__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__

and the rest of the normal methods for a class.?

I know, lots of questions, but what can i say... I am new!!

so can you guy/gals help?

thanks a million.

khalid
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091026/4cf5309c/attachment.htm>


More information about the Tutor mailing list