is there any principle when writing python function

N

Neil Cerutti

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.
 
C

Chris Angelico

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.

Oh,I definitely prefer short names to this:
http://thedailywtf.com/Articles/Double-Line.aspx

"Short" is a relative term. If the function's name is 20 characters
long and meaningful, that's fine.
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.

Chorus? Does that imply that you sing? Neat :)

What you have, I think, is a module named Cerutti, in which you have a
class of which Neil is an instance. Inside method functions, you can
be referenced by "self" (which is to code what pronouns are to
English); outside of them, you are referred to as Neil; and outside
the module, Cerutti.Neil is the cleanest way to reference you. But
your name is still Neil, no matter how you're referenced.

Chris Angelico
whose name is sometimes Chris, sometimes Rosuav, and sometimes "Chris
or Michael" by people who can't distinguish him from his brother
 
N

Neil Cerutti

Chorus? Does that imply that you sing? Neat :)

Wait... not all Python programmers sing?
What you have, I think, is a module named Cerutti, in which you
have a class of which Neil is an instance. Inside method
functions, you can be referenced by "self" (which is to code
what pronouns are to English); outside of them, you are
referred to as Neil; and outside the module, Cerutti.Neil is
the cleanest way to reference you. But your name is still Neil,
no matter how you're referenced.

The problem with that scenario is that, in real life, there's
more than one Cerutti.Neil, and they like to move around. ;)
 
C

Chris Angelico

Wait... not all Python programmers sing?

I do, and there seems to be more than coincidental overlap between
musos and coders.
The problem with that scenario is that, in real life, there's
more than one Cerutti.Neil, and they like to move around. ;)

Yes indeed; which means that your Cerutti module is in a package:

from norwich import Cerutti

It's always possible to make a locally-unique identifier into a more
globally unique one by prepending another tag to it. Alternatively,
you need to be duck-typed: you're the Neil Cerutti who writes code,
and if some other Neil Cerutti is asked to write code, he will throw
an exception. That's probably the easiest way to deal with it - but I
don't know of a way to implement it in a coded way. Maybe all names
actually point to lists of objects, and whenever you try to do
something with a name, the system goes through the elements of the
list until one doesn't fail?

Going back to the original question, the length of function name
required for it to be "meaningful" is, obviously, a variable quantity.
But I think it's still reasonable to use that as a rule of thumb for
dividing functions - if you can sanely name both halves, without
putting the entire code into the function name, then you have a case
for refactoring.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top