Nik*_*ntz 33 python reserved-words python-2.7
我可以在自己的代码中使用"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,
):
Run Code Online (Sandbox Code Playgroud)
谢谢
mod*_*che 43
使用type函数的关键字参数将掩盖函数范围内的内置函数"type".因此,虽然这样做并没有提高SyntaxError,但这不是一种好的做法,我会避免这样做.
Woo*_*ble 11
都不是.它不是一个保留字(其列表可以在http://docs.python.org/reference/lexical_analysis.html#keywords中找到),但是遮蔽任何内置函数通常是一个坏主意.
虽然其他人指出隐藏 python 内置函数是不好的形式,但只有在将函数或函数参数命名为 as 时才会出现这种情况type-
应该注意的是,type如果您将类属性命名为type.
即使在引用您的类属性时,它也总是以类实例self或自定义实例变量为前缀- 并且不会妨碍 python 内置。
例如:
>>> class SomeClass():
... type = 'foobar'
...
... def someFunction(self):
... return self.type
Run Code Online (Sandbox Code Playgroud)
>>> def type(): # Overrides python built-in in global scope
... pass
...
>>> def foobar(type):
... return type # Overrides python built-in within func
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17346 次 |
| 最近记录: |