在Python中使用多个参数执行类

在Python中使用多个参数执行类,python,class,Python,Class,新课程 我在下面指定了这个类,我正在尝试调用它。基本上,内部函数的作用是获取一个列表,从列表中删除某些数字并返回列表的长度。因此,如果我有: nums = [5,6,6,7,8], val = 6 它应该返回3的长度(列表中还有3个数字) 这是我的代码,但当我执行它时,我得到的“解决方案”对象是不可调用的: class Solution: def removeElement(self, nums, val): lens = len(nums) if le

新课程

我在下面指定了这个类,我正在尝试调用它。基本上,内部函数的作用是获取一个列表,从列表中删除某些数字并返回列表的长度。因此,如果我有:

nums = [5,6,6,7,8], val = 6
它应该返回3的长度(列表中还有3个数字)

这是我的代码,但当我执行它时,我得到的“解决方案”对象是不可调用的:

class Solution:
    def removeElement(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
            x += 1
        return x


a_solution = Solution()
a_solution([5,6,6,7,8], 6)
class Solution:
    def __call__(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
            x += 1
        return x

有两个解决方案,我相信第一个就是你想要的

解决方案1

在类中调用函数,其语法为:

a_solution.removeElement(...)
解决方案2

在类中实现
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,以使该类的实例可调用:

class Solution:
    def removeElement(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
            x += 1
        return x


a_solution = Solution()
a_solution([5,6,6,7,8], 6)
class Solution:
    def __call__(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
            x += 1
        return x

有两个解决方案,我相信第一个就是你想要的

解决方案1

在类中调用函数,其语法为:

a_solution.removeElement(...)
解决方案2

在类中实现
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
,以使该类的实例可调用:

class Solution:
    def removeElement(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
            x += 1
        return x


a_solution = Solution()
a_solution([5,6,6,7,8], 6)
class Solution:
    def __call__(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
            x += 1
        return x
要使对象“可调用”,需要定义
\uuu call\uu
方法

下面是一个简单的例子:

class Solution:
    def __init__(self, a):
        self.v = a

    def __call__(self, e):
        self.v = [x for x in self.v if x != e]
        return len(self.v)

    def __repr__(self):
        return str(self.v)
要使用代码,请执行以下操作:

s = Solution([1,2,3,6,6,5]) # initialize the list
s(6) # remove all '6'
s    # display s 
要使对象“可调用”,需要定义
\uuu call\uu
方法

下面是一个简单的例子:

class Solution:
    def __init__(self, a):
        self.v = a

    def __call__(self, e):
        self.v = [x for x in self.v if x != e]
        return len(self.v)

    def __repr__(self):
        return str(self.v)
要使用代码,请执行以下操作:

s = Solution([1,2,3,6,6,5]) # initialize the list
s(6) # remove all '6'
s    # display s 

下面是如何重写类以执行所需的操作

class Solution:
    def removeElement(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
                x += 1
        return x

# This line creates an instance of the Solution class
a_solution = Solution()
# This line calls the method removeElement
a_solution.removeElement([5,6,6,7,8], 6)
下面是另一个版本,它通过设置一些名为to_add的实例变量来利用类

class CustomSolution:

    # This method (function) gets run whenever you create an instance
    # of this class
    def __init__(self, to_add):
        # The self keyword means that each instance of the class
        # (rather than the class itself) can have it's own
        # 'to_add' variable
        self.to_add = to_add

    def removeElement(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
                x += 1
        return x + self.to_add


# Now we create an instance where the amount to add is 20
a_20_solution = Solution(to_add=20)
# This line calls the method removeElement but the result is now different!
a_20_solution.removeElement(nums=[5, 6, 6, 7, 8], val=6)  

下面是如何重写类以执行所需的操作

class Solution:
    def removeElement(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
                x += 1
        return x

# This line creates an instance of the Solution class
a_solution = Solution()
# This line calls the method removeElement
a_solution.removeElement([5,6,6,7,8], 6)
下面是另一个版本,它通过设置一些名为to_add的实例变量来利用类

class CustomSolution:

    # This method (function) gets run whenever you create an instance
    # of this class
    def __init__(self, to_add):
        # The self keyword means that each instance of the class
        # (rather than the class itself) can have it's own
        # 'to_add' variable
        self.to_add = to_add

    def removeElement(self, nums, val):
        lens = len(nums)
        if lens == 0:
            return 0
        x = 0
        for i in range(lens):
            if nums[i] != val:
                nums[x] = nums[i]
                x += 1
        return x + self.to_add


# Now we create an instance where the amount to add is 20
a_20_solution = Solution(to_add=20)
# This line calls the method removeElement but the result is now different!
a_20_solution.removeElement(nums=[5, 6, 6, 7, 8], val=6)  

您不执行类。实例化类,并执行类实例的方法。此外,程序中存在缩进问题。行
x+=1
应该在
if
语句中。
a_解决方案
不能被称为
方法
,请尝试使用@Norrius解决方案。如果您没有实际使用该类做任何事情(请注意,您的方法从不访问
self
,因此它基本上只是无关的噪音),你可能一开始就不应该上课。如果您只是想将一些相关的函数组合在一起,那么您可能需要的是一个模块,而不是执行类。实例化类,并执行类实例的方法。此外,程序中存在缩进问题。行
x+=1
应该在
if
语句中。
a_解决方案
不能被称为
方法
,请尝试使用@Norrius解决方案。如果您没有实际使用该类做任何事情(请注意,您的方法从不访问
self
,因此它基本上只是无关的噪音),你可能一开始就不应该上课。如果你只是想把一些相关的功能组合在一起,你可能需要的是一个模块。解决方案1的功劳归于@Norrius。此外,正如一些用户在评论中所说,您试图解决的问题可能不值得采用基于类的解决方案。(又称你正在用锤子在地上挖一个洞)小诡辩:这个班已经可以叫了;这就是为什么您可以编写
解决方案(…)
而不是
解决方案。定义
\uuuu调用\uuuu
使实例可调用。(事实上,
Solution(…)
之所以有效,是因为定义了
type.\uuuu call\uuuu
;因此可以调用
type
的实例,如
Solution
)。此外,正如一些用户在评论中所说,您试图解决的问题可能不值得采用基于类的解决方案。(又称你正在用锤子在地上挖一个洞)小诡辩:这个班已经可以叫了;这就是为什么您可以编写
解决方案(…)
而不是
解决方案。定义
\uuuu调用\uuuu
使实例可调用。(事实上,
Solution(…)
之所以有效,是因为定义了
type.\uu调用
;因此可以调用
type
的实例,如
Solution
。)