-4

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
user3810193
  • 53
  • 1
  • 8
  • 2
    "self is x?" - yes, `self` is `x` in that line. This should be explained in every Python `class` tutorial. – BartoszKP Jul 06 '14 at 19:00

1 Answers1

4

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>
>>>
  • thanks!! yeah i understood that self is x but didn't know how to call class. – user3810193 Jul 06 '14 at 19:10
  • i have a follow up questions if you mind helping me out. for the Heap class- i tried to call the heapify_up function. (the function compares values originally at the index child with its parents and swap the two if heap property is violated), but getting error mesg "list index is out of range". did i call the function wrong? thanks! x=Heap([8,4,6,5,7,3,2]) x.heapify_up(5)# index of child that violates the heap property print x.sorting_key – user3810193 Jul 06 '14 at 19:52