Newbie learning OOP

Steven Bethard steven.bethard at gmail.com
Sun May 29 11:46:40 EDT 2005


LenS wrote:
> class names:
>     def __init__(self, format = "F"):
>         self.format = format
> 
>     def namesplit(self, name):
>         if self.format == "F":
>             self.namelist = name.split()
>             self.first = self.namelist[0]
>             self.init = self.namelist[1]
>             self.last = self.namelist[2]
>         else:
>             self.namelist = name.split()
>             self.first = self.namelist[1]
>             self.init = self.namelist[2]
>             self.last = self.namelist[0]
> 
>         return self.first, self.init, self.last
> 
>     def fjoin(self):
>         self.namestring = self.first + ' ' + self.init + ' ' +
> self.last
> 
>     def ljoin(self):
>         self.namestring = self.last + ' ' + self.first + ' ' +
> self.init
> 
>             
> Any comments appreciated.

Seems to me like you really want a Name object, not a Names object. 
Note that fjoin() and ljoin() seem to be methods associated with an 
instance consisting of 'first', 'init' and 'last' attributes.  But your 
constructor does not provide such attributes, so a call to fjoin() or 
ljoin() immediately after creating a new names() object will fail with 
an AttributeError.  I would move the assignment of 'first', 'init' and 
'last' into the constructor, e.g. something like:

py> class Name(object):
...     def __init__(self, name, format='F'):
...         if format == 'F':
...             self.first, self.init, self.last = name.split()
...         else:
...             self.last, self.first, self.init = name.split()
...     def first_str(self):
...         return ' '.join([self.first, self.init, self.last])
...     def last_str(self):
...         return ' '.join([self.last, self.first, self.init])
...
py> n = Name('Steven John Bethard')
py> n.first_str()
'Steven John Bethard'
py> n.last_str()
'Bethard Steven John'
py> n = Name('Bethard Steven John', 'L')
py> n.first_str()
'Steven John Bethard'
py> n.last_str()
'Bethard Steven John'

You might consider passing a function instead of a string instead of the 
format parameter:

py> def first_splitter(name):
...     return name.split()
...
py> def last_splitter(name):
...     names = name.split()
...     return names[1:] + names[:1]
...
py> class Name(object):
...     def __init__(self, name, splitter=first_splitter):
...         self.first, self.init, self.last = splitter(name)
...     def first_str(self):
...         return ' '.join([self.first, self.init, self.last])
...     def last_str(self):
...         return ' '.join([self.last, self.first, self.init])
...
py> Name('Steven John Bethard').first_str()
'Steven John Bethard'
py> Name('Bethard Steven John', last_splitter).first_str()
'Steven John Bethard'

This means you don't have to keep track of a mapping between strings and 
functions; you just pass the function objects themselves.

STeVe



More information about the Python-list mailing list