Can I use the variable name “type” as function argument in Python?

前端 未结 5 1503
别那么骄傲
别那么骄傲 2020-12-07 00:44

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

def fun(name, type):
    ....
5条回答
  •  心在旅途
    2020-12-07 01:19

    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')
    

提交回复
热议问题