[Tutor] class data

Tom Jenkins tjenkins@devis.com
03 Dec 2001 11:44:53 -0500


On Mon, 2001-12-03 at 11:14, alan.gauld@bt.com wrote:
> > Everything went fine until -
> 
> Hmm, I wasn't really meaning that to be real code!
> However since Python is executable pseudo code...
> 
> > > class Entry:
> > >    def __init__(s, nm,ph,em):
> > >       s.name = nm
> > >       s.phone = ph
> > >       s.email = em
> > >    def saveme(s,flnm):
> > >       f = open(flnm,'a')
> > >       f.write(`s.name`,'\t',`s.phone`,'\t',s.email`)
> 
> Oops, try 
>           f.write(`s.name` + '\t' + `s.phone` + '\t' + s.email`)
> 

I like using variable substitution; so my version would be:
f.write('%s\t%s\t%s' % (s.name, s.phone, s.email))

or

f.write('%(nm)s\t%(ph)s\t%(em)s' % ({'nm':s.name, 'ph':s.phone,
'em':s.email}))

i prefer this as its hard to see that Alan is using backticks ( ` ) for
s.name, s.phone

-- 

Tom Jenkins
Development InfoStructure
http://www.devis.com