"Kent Johnson" <[EMAIL PROTECTED]> wrote

>> e = Explorer()
>> ....
>> e.describe(withText=True)   # gets the long version
>>
>> or
>>
>> print """
>> You are an explorer whose name is %s,
>> You have wealth of %s and strength of %s
>> """ % e.describe()  # uses tuple result
>
> Um, yuck. A function that returns either a string or a tuple 
> depending
> on its parameter? How about

Hmm, I don't have any problerm with that provided it's the same
basic information thats returned in both formats. Using a flag to
control format is quite common in report generators etc (either
postscript or pdf or plain text etc) I would agree its not good if
the actual data being returned was different. But in this case
it's a caller selectable option so there should never be any
confusion.

> class Explorer:
>   ...
>   def __str__(self):
>     fmtStr = """
> My name is %s
> and I have wealth of $%s and
> strength of %s"""
>     return fmtStr % self.values()

I thought about using the str method but decided against it
since you couldn't easily get the string version for manipulation
later - I thought - but of course you could just use the str()
convertion:

s = str(e)

So yes that would be OK.

>   def values(self):
>     return (self.name, self.wealth, self.strenth)
>
> Or get rid of values() entirely and just refer to the attributes

Nope, I don't like that as much since it encourages direct
access. And although I don't object to that occasionally - and
prefer it to writing getter methods - in the case where we are
trying to generate a report I believe the object should support
that directly by returning those values that represent it's
state - which might include some that are dynamically
calculated. - but mostly because the object (or its author)
should decide  what it wants the world to know about.

> directly - what if you want to print the values in a different 
> order?

returning a tuple means you can still access the individual
elements via indexing if you don't like the order given. But
you can (ie should) only access those attributes provided
by the values() call.

Alan G. 


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to