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

前端 未结 5 1493
别那么骄傲
别那么骄傲 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:15

    You can use _type for that. For example:

    def foo(_type):
        pass
    

    If you use type, you can't use type() in that function. For example:

    >>> type('foo')
    
    >>> type = 'bar'
    >>> type('foo')
    Traceback (most recent call last):
      File "", line 1, in 
    TypeError: 'str' object is not callable
    

提交回复
热议问题