Newbie learning OOP

John Machin sjmachin at lexicon.net
Sun May 29 16:18:01 EDT 2005


LenS wrote:
> 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?
(-:



More information about the Python-list mailing list