[Tutor] A simple question about creating a program

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Thu Mar 31 00:52:15 CEST 2005



On Wed, 30 Mar 2005, Kevin wrote:

> I was wondering, can you make a program the uses alot of classes do the
> exact same thing with out useing classes?

Hi Kevin,


Yes.  We can even do a lot of object oriented programming without classes,
although it might be slightly painful.


You asked an earlier question about constructors, so I'll try to flesh
that out too.  Let's use a small toy example.

If we have a Person class:

######
class Person:
    def __init__(self, name):
        self.name = name
        self.friends = []

    def addFriend(self, otherPerson):
        self.friends.append(otherPerson)

    def sayHelloToAll(self):
        for friend in self.friends:
            print "hi", friend.name, "it is I,", self.name
######

then we can just play with it:


######
>>> alan = Person("alan")
>>> tim = Person("tim")
>>> alan.addFriend(tim)
>>> alan.sayHelloToAll()
hi tim it is I, alan
######



But we can write fairly close code that just uses functions!

######
def makePerson():
    return {'name': None,
            'friends' : None}

def initPerson(self, name):
    self['name'] = name
    self['friends'] = []

def addFriend(self, otherPerson):
    self['friends'].append(otherPerson)

def sayHelloToAll(self):
    for friend in self['friends']:
        print "hi", friend['name'], "it is I,", self['name']
######



Let's see how this might work:

######
>>> (kevin, danny, sean) = (makePerson(), makePerson(), makePerson())
>>> initPerson(kevin, "Kevin")
>>> initPerson(danny, "Danny")
>>> initPerson(sean, "Sean")
>>> addFriend(kevin, danny)
>>> addFriend(kevin, sean)
>>> sayHelloToAll(kevin)
hi Danny it is I, Kevin
hi Sean it is I, Kevin
######

What's different here from the code with the explicit 'class' stuff is
only mere appearance.  Syntactially, it has the appearance of:

    verb(subject, object)

If we were to use classes, things would look more like:

    subject.verb(object)

which some people prefer to read.


But it's a bit clunky to do this all by hand.  As a concrete example,
Python's class support automatically does stuff like makePerson() for us:
we just have to write an appropriate initializer to shape the object the
way we want.  When we do something like this with Classes:

######
personUsingClasses = Person("Kevin")
######

we are actually doing two things: we first "construct" an object, and then
we "initialize" it to fill the object in.

In the function code above, we can see these two steps:

######
personUsingFunctions = makePerson()
initPerson(personUsingFunctions, "Kevin")
######

It's a common idiom to do construct-and-init, since an uninitialized
object is pretty useless, so Python's class support always does
construction for us.  This allows us to concentrate on the part that's
really important to us --- the initializer.


If we use Python's class support, stuff like this tends to be briefer and
easier to read.  So a little bit of Python's class support is syntax to
make it nicer to write code that focuses on "objects".

Does this make sense so far?



More information about the Tutor mailing list