is there any principle when writing python function

Neil Cerutti neilc at norwich.edu
Mon Aug 29 10:52:40 EDT 2011


On 2011-08-26, Chris Angelico <rosuav at gmail.com> wrote:
> On Sat, Aug 27, 2011 at 1:48 AM, Tobiah <tobiah at teranews.com>
> wrote:
>> While I understand and agree with that basic tenet, I think
>> that the capitalized 'ONLY' is too strong. ?I do split out
>> code into function for readability, even when the function
>> will only be called from the place from which I split it out.
>
> This can be good and can be bad. It's good when it aids
> readability; it's bad when you need to pass practically the
> entire locals() as function arguments and/or return values.

Even when lots of context is needed, defining the context with
function calls is a big improvement over directly using names in
a module's global namespace.

Sometimes repeatedly reused context suggests that creating new
classes of objects might be a good idea.

> I would split the function only when both halves (caller and
> callee) can be given short and useful names - if you can't
> explain what a block of code does in a few words, it's probably
> a poor choice for splitting out into a function.

I agree, except for the implied unconditional preference for
short names. I believe the length of a name should usually be
proportional to the scope of the object it represents.

In my house, I'm dad. In my chorus, I'm Neil. In town I'm Neil
Cerutti, and in the global scope I have to use a meaningless
unique identifier. Hopefully no Python namespace ever gets that
big.

-- 
Neil Cerutti



More information about the Python-list mailing list