Friday Finking: Limiting parameters
dn
PythonList at DancesWithMice.info
Mon Jul 13 01:21:40 EDT 2020
On 12/07/20 10:10 PM, Barry Scott wrote:
>> On 12 Jul 2020, at 00:15, DL Neil via Python-list
>> <python-list at python.org <mailto:python-list at python.org>> wrote:
>>
>>> That does not necessarily mean that the function needs to know
>>> the particular representation or form of that data. Let those be
>>> objects with getter methods for the data you wish, and have the
>>> function document what methods it will attempt to call. Then
>>> any class that provides the expected methods would be suitable.
>>
>> +1
>>
>> Here, the proposal is not to pass an object per-se, but to pass a
>> function/method ("getter method") which will deliver the requisite
>> data-items?
>>
>> So, might we then find that our mailing-label routine includes
>> something like (copy-pasting is NOT proper Python!):
>>
>> def mail_label( getter ):
>> ...
>> ( first name, middle initial, last name, house number, street name,
>> apartment number, town, state, country, zip code ) = getter()
>>
>> In which case, have we not moved the "very long list" from the
>> function def to a later line within the routine - and how is this in
>> some way 'better'?
>>
>
> This is not the refactor that Roger's excellent rule-of-thumb implies.
>
> Clearly moving the 20 positional args into a tuple is basically the same
> code,
> and the same maintenance problem.
Agreed!
> I'd expect to see something like this:
>
> def mail_label( person, address ):
> first_name = person.first_name
> # or if you want a function interface
> first_line_of_address = address.get_first_line()
Does this idea move whole objects across the interface? (see earlier in
the thread) Taking a person class as subject and a mailing-label as the
function's object, aren't there two criticisms?
1 the function needs to know which attributes of the person and address
objects it wants to use, cf saying 'I need street, number, town, ...'*
2 "person" likely contains a load of data that is irrelevant to printing
a mail-label.
Why is this preferable to using a 'getter' method, or some other approach?
* your point (above) being that these need not be (say) a dozen data
elements, but could be just-enough data, and data-combinations which
identify the addressee and which represent the components of the address.
--
Regards =dn
More information about the Python-list
mailing list