Newbie learning OOP

L

LenS

Trying to learn OOP concepts and decided to use Python for this
purpose. I have coded the following CLASS and it seems to work fine.
Any comments on the code or suggestions would be appreciated.

The class let you take a person's name and split it up into first last
and middle. The class defaults to the assumption that the name will be
passed in as a string in first last and middle order however you can
set the format attribute to take last first and middle order. You can
then get a string back in and opposite order.

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.

Len Sumnler
 
S

Steven Bethard

LenS said:
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
 
J

John Machin

LenS said:
Trying to learn OOP concepts and decided to use Python for this
purpose. I have coded the following CLASS and it seems to work fine.
Any comments on the code or suggestions would be appreciated.

The class let you take a person's name and split it up into first last
and middle. The class defaults to the assumption that the name will be
passed in as a string in first last and middle order however you can
set the format attribute to take last first and middle order. You can
then get a string back in and opposite order.

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

You are missing 'return' in the fjoin and ljoin methods.

A practical problem: not everbody's name can be shoe-horned into the
first/initial/last model. You face catastrophic loss of information.

Some examples, with "last" name in capitals:

J. Edgar HOOVER -> J E HOOVER
Rip J. VAN WINKLE -> Rip J VAN
Jean Paul DE LA SALLE -> Jean P DE
DE LA SALLE, Jean Paul -> LA S DE
MAO Tse Tung -> Tse T MAO # or MAO T Tung
MAO Tse-tung -> Tse-tung M <empty> # or an IndexError
MAO Zedong -> Zedong M <empty> # or an IndexError
Vigdis ERIKSDOTTIR -> Vigdis E <empty> # and lost the gender, too
Gunnlaug ILLUGASON Ormstunga -> Gunnlaug I Ormstunga # nickname
"Snakestongue"
Ivan Denisovich SHUKHOV -> Ivan D SHUKHOV # and lost father's "first"
name, too
Friedrich Heinrich Karl VON UND ZU HOHENLOHE -> Friedrich H Karl
NGUYEN Thi Thanh Van -> Thi T NGUYEN
# "Thi" means "female" and "Nguyen" is the "last" name of about 50% of
the Vietnamese population ...

:)
You don't work for the Department of Homeland Security, do you?
(-:
 
L

LenS

Thanks for the reply. I will be looking over you code. I'm trying to
learn OOP. It just hasn't sunk in yet;-\
 
L

LenS

You are correct, the code so far is just a simple problem to learn OOP.
Oh by the way I don't work for Dept. of Homeland Security just
remember "drop those fingernail clipers and step away from those
shoes:)
 
T

Terry Hancock

A practical problem: not everbody's name can be shoe-horned into the
first/initial/last model. You face catastrophic loss of information.

Some examples, with "last" name in capitals:

J. Edgar HOOVER -> J E HOOVER
Rip J. VAN WINKLE -> Rip J VAN
Jean Paul DE LA SALLE -> Jean P DE
DE LA SALLE, Jean Paul -> LA S DE
MAO Tse Tung -> Tse T MAO # or MAO T Tung
MAO Tse-tung -> Tse-tung M <empty> # or an IndexError
MAO Zedong -> Zedong M <empty> # or an IndexError
Vigdis ERIKSDOTTIR -> Vigdis E <empty> # and lost the gender, too
Gunnlaug ILLUGASON Ormstunga -> Gunnlaug I Ormstunga # nickname
"Snakestongue"
Ivan Denisovich SHUKHOV -> Ivan D SHUKHOV # and lost father's "first"
name, too
Friedrich Heinrich Karl VON UND ZU HOHENLOHE -> Friedrich H Karl
NGUYEN Thi Thanh Van -> Thi T NGUYEN
# "Thi" means "female" and "Nguyen" is the "last" name of about 50% of
the Vietnamese population ...

While I might say something snide about unnecessarily crucifying a piece
of "toy" code for learning OOP, this is *really* nice set of test cases! Thank
you, I'm saving this for reference. :)

Cheers,
Terry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top