seek operation in python
Larry Hudson
orgnut at yahoo.com
Thu Apr 30 22:42:24 EDT 2015
On 04/30/2015 01:50 PM, Cecil Westerhof wrote:
> Op Thursday 30 Apr 2015 21:38 CEST schreef Larry Hudson:
>
>> On 04/30/2015 01:06 AM, Cecil Westerhof wrote:
>> [snip]
>>
>>> I wrote a module where I have:
>>> def get_indexed_message(message_filename, index):
>>> """
>>> Get index message from a file, where 0 gets the first message
>>> """
>>>
>>> return open(expanduser(message_filename),
>>> 'r').readlines()[index].rstrip()
>>>
>> Very minor nit unrelated to the file closing question:
>> This would be a good place to give index a default value...
>>
>> def get_indexed_message(message_filename, index=0):
>
> Why would you want this? I think the index should always be defined.
>
Matter of opinion, of course, but this allows calling the function without specifying the index
and you still get a useful (and logical) result. I find this convenient in most similar cases.
Using default parameters is quite Pythonic. Note that this _permits_ using the function this
way, it does not _require_ it. Of course, it also should be suitably documented. Naturally, YMMV.
-=- Larry -=-
More information about the Python-list
mailing list