[Tutor] designing POOP
Kent Johnson
kent37 at tds.net
Sun Feb 10 02:42:04 CET 2008
Alan Gauld wrote:
>> 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.
Maybe we just need to agree to disagree here. I don't see how returning
a tuple is better encapsulation than direct access to named attributes.
And what if an attribute is added? Do you add another value to the
tuple, forcing every client to change? Start returning a value object
with named attributes? :-)
>> 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.
So to print, say, strength, wealth and name, we can have
values = explr.values()
print values[2], values[1], values[0]
or unpack the tuple and give them names again:
strength, wealth, name = explr.values()
print strength, wealth, name
or just use the perfectly good names they already have:
print explr.strength, explr.wealth, explr.name
and of course unless you take extra steps to prevent it, direct
attribute access is still available even with the values() method...
Kent
More information about the Tutor
mailing list