Is it safe to use the python word “type” in my code?

左心房为你撑大大i 提交于 2019-11-27 14:34:44

Using type as a keyword argument to a function will mask the built-in function "type" within the scope of the function. So while doing so does not raise a SyntaxError, it is not considered good practice, and I would avoid doing so.

Neither. It's not a reserved word (a list of which can be found at http://docs.python.org/reference/lexical_analysis.html#keywords ), but it's generally a bad idea to shadow any builtin.

type should absolutely be consider a reserved word. While it can be tempting to use this word for database fields, consider that the fact that type() is one of the most important debugging/ testing functions because it tells you the class of an object.

$ python

>>> x = 5
>>> s = "rockets"
>>> y = [1,2,3] 

>>> print(type(x)) 
class 'int'

>>> print(type(s))
class 'str'

>>> print(type(y)) 
class 'list'

An atlas would be classified a type of book, but consider using the word "category" instead.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!