在我的代码中使用python单词“type”是否安全?

时间:2021-06-07 20:29:46

Can I use the word "type" in my own code or is it reserved? My function header:

我可以在自己的代码中使用“type”这个词还是保留?我的函数头:

def get(
    self,
    region='Delhi',
    city='Delhi',
    category='Apartments',
    type='For sale',
limit = 60,
    PAGESIZE=5,
    year=2012,
    month=1,
    day=1,
    next_page=None,
threetapspage=0,
    ):

Thank you

2 个解决方案

#1


34  

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.

使用type作为函数的关键字参数将掩盖函数范围内的内置函数“type”。因此,虽然这样做不会引发SyntaxError,但它不被认为是好的做法,我会避免这样做。

#2


9  

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.

都不是。它不是一个保留字(其列表可以在http://docs.python.org/reference/lexical_analysis.html#keywords中找到),但是遮蔽任何内置函数通常都是一个坏主意。

#1


34  

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.

使用type作为函数的关键字参数将掩盖函数范围内的内置函数“type”。因此,虽然这样做不会引发SyntaxError,但它不被认为是好的做法,我会避免这样做。

#2


9  

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.

都不是。它不是一个保留字(其列表可以在http://docs.python.org/reference/lexical_analysis.html#keywords中找到),但是遮蔽任何内置函数通常都是一个坏主意。