Python 将列表传递给类中的函数
我正在学习Python。我不知道如何将列表传递给类中的函数。以下是我的例子:Python 将列表传递给类中的函数,python,list,function,Python,List,Function,我正在学习Python。我不知道如何将列表传递给类中的函数。以下是我的例子: class Solution: def fn_one(self, strs = []): print(strs[0]) return strs[0] strs = ["ab", "abc", "abcs", "abx"] x = Solution.fn_one(strs) 输出: 索引器:列表索引超出范围 就像我在传递一张空名单。我做错了什么?因为fn\u one是一个
class Solution:
def fn_one(self, strs = []):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution.fn_one(strs)
输出:
索引器:列表索引超出范围
就像我在传递一张空名单。我做错了什么?因为
fn\u one
是一个实例方法,所以您将strs
作为self
传递,您可以创建解决方案的一个实例,将其作为self传递,并将strs
作为strs
列表传递:
x = Solution().fn_one(strs)
输出:
ab
除非这不是您想要的,否则您可以使用@staticmethod
装饰器将fn_one
定义为静态方法:
class Solution:
@staticmethod
def fn_one(strs = []): # here, nothing is passed automatically
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
# Solution().fn_one(strs) would work also, since nothing is passed automatically
class Solution:
@classmethod
def fn_one(cls, strs = []): # here, the class is passed automatically as `cls`
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
或者,作为类方法,使用@classmethod
装饰器:
class Solution:
@staticmethod
def fn_one(strs = []): # here, nothing is passed automatically
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
# Solution().fn_one(strs) would work also, since nothing is passed automatically
class Solution:
@classmethod
def fn_one(cls, strs = []): # here, the class is passed automatically as `cls`
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
但是,如果类和对象都不是方法正常工作所必需的,那么使用静态方法是最好的方法。如果您的类中没有任何处理对象的方法,那么就不需要类,只需使用函数即可。因为fn\u one
是一个实例方法,所以您要将strs
作为self
传递,您可以创建解决方案的一个实例,以便将其作为self传递,以及作为strs
列表传递的strs
:
x = Solution().fn_one(strs)
输出:
ab
除非这不是您想要的,否则您可以使用@staticmethod
装饰器将fn_one
定义为静态方法:
class Solution:
@staticmethod
def fn_one(strs = []): # here, nothing is passed automatically
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
# Solution().fn_one(strs) would work also, since nothing is passed automatically
class Solution:
@classmethod
def fn_one(cls, strs = []): # here, the class is passed automatically as `cls`
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
或者,作为类方法,使用@classmethod
装饰器:
class Solution:
@staticmethod
def fn_one(strs = []): # here, nothing is passed automatically
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
# Solution().fn_one(strs) would work also, since nothing is passed automatically
class Solution:
@classmethod
def fn_one(cls, strs = []): # here, the class is passed automatically as `cls`
print(strs[0])
return strs[0]
X = Solution.fn_one(strs)
但是,如果类和对象都不是方法正常工作所必需的,那么使用静态方法是最好的方法。如果您的类中没有任何处理对象的方法,那么就不需要类,只需使用函数即可。在fn_one
方法上方添加一个@classmethod
装饰器,或者先像这样实例化类:x=Solution().fn_one(strs)
在fn\u one
方法上方添加一个@classmethod
装饰器,或者先实例化类,如下所示:x=Solution().fn\u one(strs)
在使用fn\u one
方法之前,您应该创建一个instance。像这样:
代码:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
输出:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
或者,如果你把括号放在课后,你可以在一行中完成。像这样:
代码:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
输出:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
您应该在使用fn\u one
方法之前创建一个instance。像这样:
代码:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
输出:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
或者,如果你把括号放在课后,你可以在一行中完成。像这样:
代码:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
输出:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
问题是,您调用fn_one就像调用静态方法一样,因此您在方法self
中接收字符串列表
您有几个选择:
创建解决方案的实例
solution=solution()
x=解决方案。fn\u一(strs)
或者更改类定义,使fn_one成为静态方法
class Solution:
@staticmethod
def fn_one(strs=[]):
print(strs[0])
return strs[0]
问题是,您调用fn_one就像调用静态方法一样,因此您在方法self
中接收字符串列表
您有几个选择:
创建解决方案的实例
solution=solution()
x=解决方案。fn\u一(strs)
或者更改类定义,使fn_one成为静态方法
class Solution:
@staticmethod
def fn_one(strs=[]):
print(strs[0])
return strs[0]
试试这个代码
首先创建solution类的实例&调用fn_one()函数及其引用
代码:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
输出:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
试试这个代码
首先创建solution类的实例&调用fn_one()函数及其引用
代码:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
输出:
strs = ["ab", "abc", "abcs", "abx"]
x = Solution()
x.fn_one(strs)
>>> python3 test.py
ab
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
>>> python3 test.py
ab
class Solution:
def fn_one(self, strs):
print(strs[0])
return strs[0]
strs = ["ab", "abc", "abcs", "abx"]
x = Solution().fn_one(strs)
ab
你为什么要在这里上课?你的类没有内部状态,它是没有意义的。另外,您不需要实例化它的实例,而是直接尝试调用类上的函数,因此strs
被传递给self
(因为它实际上就是您定义的函数)。最后,当使用可变对象作为默认值时,应该注意,默认值在函数定义时只计算一次。如果您改变了默认值,那么在使用默认值的每个调用中都会看到变化…您应该从开始。为什么在这里使用类?你的类没有内部状态,它是没有意义的。另外,您不需要实例化它的实例,而是直接尝试调用类上的函数,因此strs
被传递给self
(因为它实际上就是您定义的函数)。最后,当使用可变对象作为默认值时,应该注意,默认值在函数定义时只计算一次。如果你改变了默认值,那么在每次使用默认值的调用中都会看到变化……你应该从“但是如果类和对象都不是方法正常工作所必需的,那么使用静态方法是最好的方法。”在这种情况下,最好的方法是根本不要使用方法。…。@juanpa.arrivillaga你是对的,我的意思是方法fn_one
,代码中可能没有显示其他方法可以处理实例。但是如果类和对象都不是方法正常工作所必需的,那么使用静态方法是最好的方法。“在这种情况下,最好的办法是根本不要使用方法.....。@juanpa.arrivillaga你是对的,我的意思是方法fn_one
,可能还有代码中没有显示的其他方法可以处理实例。