Beginning Question about Python functions, parameters...
astral orange
457r0.jp at gmail.com
Tue Nov 24 10:45:00 EST 2009
On Nov 23, 10:37 pm, r <rt8... at gmail.com> wrote:
> On Nov 23, 11:19 am, astral orange <457r0... at gmail.com> wrote:
>
>
>
> > Hi, I am trying to teach myself Python and have a good book to help me
> > but I am stuck on something and I would like for someone to explain
> > the following piece of code for me and what it's actually doing.
> > Certain parts are very clear but once it enters the "def store(data,
> > full_name): ...." function and the "def lookup()..." function things
> > get a little confusing for me. Specifically, lines 103-108 *and* Lines
> > 110-111.
>
> > Lastly, I am not sure how to print the results I've put into this
> > program either, the book I'm reading doesn't tell me. As you can tell,
> > I am a beginner and I don't truly understand everything that is going
> > on here...a lot, but not all....
>
> > Here is the code:
>
> > 92 def init(data):
> > 93 data['first'] = {}
> > 94 data['middle'] = {}
> > 95 data['last'] = {}
> > 96
> > 97 def store(data, full_name):
> > 98 names = full_name.split()
> > 100 if len(names) == 2: names.insert(1, '')
> > 101 labels = 'first', 'middle', 'last'
> > 103 for label, name in zip(labels, names):
> > 104 people = lookup(data, label, name)
> > 105 if people:
> > 106 people.append(full_name)
> > 107 else:
> > 108 data[label][name] = [full_name]
> > 109
> > 110 def lookup(data, label, name):
> > 111 return data[label].get(name)
> > 112
> > 113
> > 114 MyNames = {}
> > 115 init(MyNames)
> > 116 store(MyNames, 'John Larry Smith')
> > 117 lookup(MyNames, 'middle', 'Smith')
>
> This is a horrible example to show noobs. I think the OP could better
> understand this as a class EVEN though the OP may or may not know what
> a class *is* yet.
>
> >>> class Name():
>
> def __init__(self, first, middle, last):
> self.first = first
> self.middle = middle
> self.last = last
>
> >>> name1 = Name('Guido', 'van', 'Rossum')
> >>> name1.first
> 'Guido'
> >>> name1.middle
> 'van'
> >>> name1.last
> 'Rossum'
> >>> print name1
>
> <__main__.Name instance at 0x029BFD78>
>
> This time we add a __str__ method, the result will speak louder than
> words!!
>
> >>> class Name():
>
> def __init__(self, first, middle, last):
> self.first = first
> self.middle = middle
> self.last = last
> def __str__(self):
> return '%s %s %s' %(self.first, self.middle, self.last)
>
> >>> name2 = Name('Terry', 'J', 'Reedy')
> >>> name2.first
> 'Terry'
> >>> name2.middle
> 'J'
> >>> name2.last
> 'Reedy'
> >>> print name2
>
> Terry J Reedy
>
> See the difference in the print statements. Now lets have some real
> fun and access each sub name by index haha!
>
> >>> class Name():
>
> def __init__(self, first, middle, last):
> self.first = first
> self.middle = middle
> self.last = last
> def __str__(self):
> return '%s %s %s' %(self.first, self.middle, self.last)
> def __len__(self):
> return 3
> def __getitem__(self, item):
> if item == 0:
> return self.first
> elif item == 1:
> return self.middle
> elif item == 2:
> return self.last
> else:
> raise IndexError("Index must be in range 0, 2")
>
> >>> name = Name('Joe', 'blow', 'scripter')
> >>> name[0]
> 'Joe'
> >>> name[1]
> 'blow'
> >>> name[2]
> 'scripter'
> >>> len(name)
>
> 3
>
> WOW, thats more info in a few lines than any tut i ever seen! I wish i
> could have seen that in my initial days, could have save some
> countless hours of confusion!!! Maybe i am in the wrong line of work?
>
> Should i keep going...?
Yeah, I don't think the example in the book is the best for someone
starting out. I still
am "not getting" certain parts of the program so I think I'll move on
in hopes that it will
*not* came back to haunt me and the book (along with the online
tutorial) will help me grab
more of the basics of Python programming.
As for the "class Name():" example above? Even though I haven't seen
exactly what purpose 'self' serves
yet I can follow and understand what is going on very easily, that
helps out tremendously.
Very clearly written...Thank you!
And thanks again to everyone...
More information about the Python-list
mailing list