http://i.stack.imgur.com/IB0Yq.png
我是编码的新手,不知道为什么列表被输入为元组。
答案 0 :(得分:4)
对于内置类型和局部变量,您使用了相同的名称list
。不要重复使用内置名称。引用PEP-8:
如果函数参数的名称与保留关键字冲突,通常最好附加单个尾随下划线而不是使用缩写或拼写损坏。因此
class_
优于clss
。 (或许最好是通过使用同义词来避免这种冲突。)
尝试:
def funct2(list_):
if type(list_) == list:
...
或者,更好:
def funct2(list_):
if isinstance(list_, list):
...
答案 1 :(得分:0)
def function2(list_variable):
#: dont use names that can shadow built in types
#: like `int`, `list` etc
if isinstance(list_variable, list):
#: do something here
list_variable.append(["a"]) #: to append a list i.e. ['b', ['a']]
list_variable + ["a"] #: also works but... `your cup of tea`
elif isinstance(list_variable, tuple):
#: you are trying to add a list `[]` to a tuple `("a",)`
#: this is not allowed, how does the computer know if it is to
#: convert your list to a tuple or your tuple to a list.
#: this conversion is called coercion look it up
#: Also read about immutability
list_variable + ("a", )
else:
print("Sorry, only lists or tuples allowed")