4

Is using the word function for the name of an argument considered bad style in Python code?

def conjunction_junction(function):
    pass # do something and call function in here

This pattern occurs all the time, especially in decorators. You see func, fn and f used all of the time but I prefer to avoid abbreviations when possible. Is the fact that it's the name of a type enough to warrant abbreviating it?

>> type(conjunction_junction).__name__
'function'
6
  • As long as it doesn't override anything and it doesn't cause syntax color issues (it doesn't) - I personally don't mind. Commented Jun 9, 2012 at 2:02
  • function does highlight in blue here on stack overflow :) Commented Jun 9, 2012 at 2:03
  • @Mike: That's because SO uses a generic highlighter. Let me add the language tag so it highlights properly. Commented Jun 9, 2012 at 2:17
  • @NiklasB. Thanks! I'll do that in the future. Commented Jun 9, 2012 at 2:32
  • 1
    > I prefer to avoid abbreviations when possible < A great philosophy (mine as well)! There's no problem with using function, but given how common func is, I wouldn't be against abbreviating this specific name. Commented Jun 9, 2012 at 2:34

2 Answers 2

3

It's not a reserved keyword, so I don't see why not.

From the Style Guide

If a function argument's name clashes with a reserved keyword, it is generally better to append a single trailing underscore rather than use an abbreviation or spelling corruption. Thus class_ is better than clss. (Perhaps better is to avoid such clashes by using a synonym.)

Sign up to request clarification or add additional context in comments.

1 Comment

str and list are also no reserved keywords. You still want to avoid using them as identifiers. Of course in this case, function is also not a builtin class, module or function name, so it's okay to use it (though I've seen func being used more commonly, maybe for the exact same reason that OP started the question: they weren't sure whether it's okay to use function)
3

Using function is perfectly fine.

There is nothing in the style guide about it specifically. The reason that the use of type names such as str and list is highly discouraged is because they have functionality within the language. Overwriting them would obscure the functionality of the code. function on the other hand, does nothing.

I suspect func, fn, and f are used because they are all shorter than typing function ;)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.