0

I am reading through Lutz's Programming Python and the following script is designed to create three records of people, make them persistent by storing them in a shelve, and then allow a user to enter people names in the terminal to get information back. I don't have all the code here, just what I think is necessary to understand.

Lines 8-13 show the class used in creating each person. These are then written to a shelf called class-shelve shown on line 15. The two lines I'm having trouble understanding are 17 and 29/30.

17 I am not sure of the logic here.

29 is the line from the book, and I don't understand how the code i try on 30 is any different?

  6 import shelve 
  7     
  8 class Person:
  9     def __init__(self, name, age, pay=0, job=None):
 10             self.name = name
 11             self.age = age
 12             self.pay = pay 
 13             self.job = job
 14 
 15 classShelve = './output/class-shelve'
 16 fieldnames = ('name', 'age', 'job', 'pay')
 17 maxfield = max(len(f) for f in fieldnames)
 18 db = shelve.open(classShelve)
 19 
 20 while True:
 21     key = input('\nKey? => ')
 22     if not key: break
 23     try:
 24         record = db[key]
 25     except:
 26         print('No such key "%s"!' % key)
 27     else:
 28         for field in fieldnames:
 29             #print(field.ljust(maxfield), '=>', getattr(record, field))
 30             print(field, '=>', getattr(record, field))
4
  • 4
    17 gives you the maximum length of the longest fieldname. It is decipherable almost in plain English. max(len(f) for f in fieldnames) => the max len(gth) of f for (all of the) f in fieldnames. For 29 v 30, there isn't really a difference, except the use of ljust. Is that your question? Commented Nov 20, 2015 at 21:39
  • That makes sense thanks. And yeah i have a basic understanding of what ljust is doing, but both lines 29/30 give me the same output and doesn't change justification. Commented Nov 20, 2015 at 21:45
  • It just makes the table cleaner. ljust and rjust don't do anything if the length of the string is already >= the argument they're given. Compare 'hello'.ljust(5) and 'hello'.ljust(20) (note all the trailing space after the latter, or use rjust if it's easier to see). Commented Nov 20, 2015 at 21:47
  • Oh cool that makes sense now. I appreciate the help. Commented Nov 20, 2015 at 21:49

1 Answer 1

2

Line 17:

maxfield = max(len(f) for f in fieldnames)

this applies the max function over the list created by the comprehension len(f) for f in fieldnames

len(f) for f in fieldnames

could also be written as

[len(f) for f in fieldnames]

which means: create a list of the lengths of all the items in fieldnames.

max(len(f) for f in fieldnames)

means: find the biggest item in fieldnames.

Line 29/30

Your question is not so clear. I'm assuming that you are asking what the following function does?

field.ljust(maxfield)

According to this reference, string.ljust pads out a string to the given length so as to left justify it. By default, it pads with spaces. Perhaps that's why you're not seeing anything. If you supply it with a second argument, it uses that to pad the string. If the length of the string is greater than the supplied length, it just returns the string without modifying it.

Sign up to request clarification or add additional context in comments.

1 Comment

Slightly misleading breakdown of max(len(f) for f in fieldnames) because the comprehension doesn't return a list there. Without the [] inside a function call that becomes an implied generator expression, returning values one at a time. Otherwise, great answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.