Lou*_*mka 1 python oop methods list
我有一个Python的类方法,如下所示:
def func(indel, nind=indel.length):
#do_something
Run Code Online (Sandbox Code Playgroud)
indel是一个列表.
我在使用此函数定义时收到错误.有什么办法可以吗?
虽然我想知道您为什么要使用参数,但您可能希望这样做,因为传递列表的长度是可选的,例如,如果您想要对列表的一小部分进行操作.如果是这种情况,这是我将使用的代码:
def func(indel, length = None):
#do_something
if length is None: # By default, use the length of the list
length = len(indel)
# do whatever with indel and length
# test methods
test = [1, 2, 3, 4, 5]
func(test) # in this case, length = 5
func(test, 1) # in this case, length = 1
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
106 次 |
| 最近记录: |