类__init __()只需要2个参数(1给出) [英] class __init__() takes exactly 2 arguments (1 given)

查看:483
本文介绍了类__init __()只需要2个参数(1给出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Python中的类。运行两个例子,两个函数都有2个参数(其中一个给出),但是,只有一个例子执行,但另一个不执行。



虽然 x.setdata()只有1个参数。
class FirstClass:
def setdata(self,value):
self.data = value
def display(self):
print(self.data) p>

  #make两个实例
x = FirstClass()
y = FirstClass()

x.setdata(king arthur)#call方法:self is x ??
y.setdata(3.31212)

x.display()
y.display()

这个不运行,错误 msg:__init __()只需要2个参数(1给定)



class Heap:

def __init __(self,sorted_key):
初始化一个新实例

Args:
sort_key:指定将
插入到堆中的对象的属性,创建堆的基础

self.heap = list()
self.mapping = dict()
self.sorting_key = sort_key

def heapify_up(self,child):
标准的heapify操作,如在CLRS中所描述的
通过将元素原始地交换到堆中的索引孩子
与其父直到满足堆属性的工作。修改
合适的堆属性

Args:
child:违反堆属性的元素的索引

返回:


parent =(child - 1)/ 2
#将最初在索引子项处的元素与其父项
#交换,直到父项的指定属性的值大于
#比元素本身的指定属性的值
while(getattr(self.heap [parent],self.sorting_key)<
getattr(self.heap [child],self.sorting_key)):
if(parent == -1):
#这意味着孩子是0,这意味着我们已经达到
#堆的顶部
return

#交换映射,以确保未来在
#中的引用映射dict指向对象的正确位置
#heap
self.mapping [self.heap [parent] .player] = child
self.mapping [self.heap [child] .player] = parent

#交换父代和子代
temp = self.heap [parent]
self.heap [parent] = self.heap [child]
self.heap [child] temp

#将子和父指针移到堆上
child = parent
parent =(child - 1)/ 2

$ b bx = Heap()
x .__ init __([42,22,54,1,3])

x.heapify_up(1,5)
print x


解决方案

这两行:

  x = Heap()
x .__ init __([42,32,54,1,3])

应为一个:

  x =堆[42,32,54,1,3])

请记住,堆当你执行 Heap()时,隐式调用.__ init __ 。下面是一个演示:

 >>> class Foo:
... def __init __(self):
... print(Foo .__ init__ was called)
...
>> Foo()
Foo .__ init__被调用
< __ main__.Foo对象在0x020AF030>
>>>>


I'm learning about classes in Python. Running two examples, both with function have 2 arguments (1 of which is given), however, only one examples executes but the other doesn't.

This one executes even though x.setdata() only has 1 arguments. class FirstClass: def setdata(self,value): self.data=value def display(self): print(self.data)

#make two instances
x=FirstClass()
y=FirstClass()

x.setdata("king arthur") #call methods: self is x?? 
y.setdata(3.31212)

x.display()
y.display()

This one doesn't run, error msg: __init__() takes exactly 2 arguments (1 given):

class Heap:

        def __init__(self, sorting_key):
            """Initializes a new instance of a heap.

            Args:
                sorting_key: Specifies the attribute of the object inserted
                into the heap, on the basis of which the heap was created.
            """
            self.heap = list()
            self.mapping = dict()
            self.sorting_key = sorting_key

        def heapify_up(self, child):
                """Standard heapify operation, as described in CLRS.
                Works by swapping the element originially at index child in the heap
                with its parent until the heap property is satisfied. Modifies the
                appropriate heap attribute

                Args:
                    child: Index of the element that violates the heap property

                Returns:
                    None
                """
                parent = (child - 1) / 2
                # Swaps the element originally at the index child with its parent
                # until the value of the specifed attribute of the parent is greater
                # than the value of the specified attribute of the element itself
                while (getattr(self.heap[parent], self.sorting_key) <
                       getattr(self.heap[child], self.sorting_key)):
                    if (parent == -1):
                        # This means child was 0, which means we have reached the
                        # top of the heap
                        return

                    # Swap the mappings as well to ensure that future references in
                    # the mapping dict refer to the correct position of the object in
                    # the heap
                    self.mapping[self.heap[parent].player] = child
                    self.mapping[self.heap[child].player] = parent

                    # Swap the parent and the child
                    temp = self.heap[parent]
                    self.heap[parent] = self.heap[child]
                    self.heap[child] = temp

                    # Move the child and parent pointers up the heap
                    child = parent
                    parent = (child - 1) / 2


    x=Heap()
    x.__init__([42,32,54,1,3])

    x.heapify_up(l,5)
    print x

解决方案

These two lines:

x=Heap()
x.__init__([42,32,54,1,3])

should be one:

x=Heap([42,32,54,1,3])

Remember that Heap.__init__ is called implicitly when you do Heap(). Below is a demonstration:

>>> class Foo:
...     def __init__(self):
...         print("Foo.__init__ was called")
...
>>> Foo()
Foo.__init__ was called
<__main__.Foo object at 0x020AF030>
>>>

这篇关于类__init __()只需要2个参数(1给出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆