tags:

views:

99

answers:

4

Can I use type as name for a function argument?

def fun(name, type):
    ....

Thanks, Boda Cydo.

+8  A: 

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.

jathanism
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
A: 

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

Francesco
+1  A: 

You can use any non-keyword that follows the identifier rules as an identifier. type is not a keyword, but using it will shadow the type built-in.

Ignacio Vazquez-Abrams
+2  A: 

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.

Roger Pate
+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