28

Can I use type as a name for a python function argument?

def fun(name, type):
    ....
simanacci
  • 2,197
  • 3
  • 26
  • 35
bodacydo
  • 75,521
  • 93
  • 229
  • 319

5 Answers5

43

You can, but you shouldn't. It's not a good habit to use names of built-ins because they will override the name of the built-in in that scope. If you must use that word, modify it slightly for the given context.

While it probably won't matter for a small project that is not using type, it's better to stay out of the habit of using the names of keywords/built-ins. The Python Style Guide provides a solution for this if you absolutely must use a name that conflicts with a keyword:

single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.

Tkinter.Toplevel(master, class_='ClassName')
wjandrea
  • 28,235
  • 9
  • 60
  • 81
jathanism
  • 33,067
  • 9
  • 68
  • 86
  • 19
    BTW, since type is such a generic, oft' overused word, adding a qualifer as in "car_type" or "animal_type" makes for a more self-documenting program anyway – mjv Mar 10 '10 at 15:23
  • 1
    i used to use `tipe`, but I always hated the way it looked. now I use `typ` – rbp Jan 16 '16 at 16:02
  • @Bloke, i think it make sense to name it like `typ`. If u have a class `car`, u don't want to have a property `car_type`. A language shouldn't steal generic names. – Mengo Jun 26 '17 at 18:51
  • Extending the advice from PEP 8 to cover builtins, you could use `type_` – wjandrea Jul 02 '20 at 01:59
7

You can, and that's fine. Even though the advice not to shadow builtins is important, it applies more strongly if an identifier is common, as it will increase confusion and collision. It does not appear type will cause confusion here (but you'll know about that more than anyone else), and I could use exactly what you have.

  • 5
    +1 for mentioning potential confusion. I think that's a much bigger issue than the technical problem of possibly shadowing a builtin: someone reading the code (even possibly the person who wrote it) might expect `type` to refer to the builtin. The chances of accidentally shadowing the builtin `type` seem small in comparison: it's not used all that often in real code. – Mark Dickinson Mar 10 '10 at 15:34
5

You can use any non-keyword as an identifier (as long as it's a valid identifier of course). type is not a keyword, but using it will shadow the type built-in.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

You can, but you would be masking the built-in name type. So it's better not to do that.

Francesco
  • 3,200
  • 1
  • 34
  • 46
0

You can use _type for that. For example:

def foo(_type):
    pass

If you use type, you can't use type() in that function. For example:

>>> type('foo')
<type 'str'>
>>> type = 'bar'
>>> type('foo')
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: 'str' object is not callable
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Shameem
  • 2,664
  • 17
  • 21
  • 1
    It would probably be better to use `type_`, like PEP 8 recommends for [avoiding conflicts with keywords](https://www.python.org/dev/peps/pep-0008/#descriptive-naming-styles) – wjandrea Jul 02 '20 at 02:07