Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建类的对象以测试python方法_Python_Object - Fatal编程技术网

创建类的对象以测试python方法

创建类的对象以测试python方法,python,object,Python,Object,从Leetcode中的一个问题来看,这个解决方案是正确的,但希望通过创建一个实例,然后打印方法twoSum的结果,自己在VSCode中对其进行测试。但我不知道该怎么做 class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[in

从Leetcode中的一个问题来看,这个解决方案是正确的,但希望通过创建一个实例,然后打印方法twoSum的结果,自己在VSCode中对其进行测试。但我不知道该怎么做

class Solution(object):
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        count1 = 0
        for i in nums:
            count2 = 0
            for j in nums:
                if (i+j) == target and i != j:
                    return count1, count2
                count2 +=1
                
            count1 +=1


if __name__ == "__main__":
    print(twoSum([2,7,11,15],9 ))

感谢您的帮助

您首先必须通过执行以下操作来实例化
解决方案
实例-

s=Solution()
然后调用该方法-

s.twoSum([2, 7, 11, 15], 9)