-2

I'm using 'an illustrated guide to learning python 3' to learn python. Chapter 21 is about classes. In this chapter it uses 'self' aparently incorrectly? I tried writing my own code for an example, and it didn't work, so I input the example code and, surprisingly, it did not work either.

class CorrectChair:
    '''blah'''
    max_occupants = 4

    def __init__(self, id):
        self.id = id
        self.count = 0

    def load(self, number):
        new_val = self.check(self.count + number)
        self.count = new_val

    def unload(self, number):
        new_val - self._check(self.count - number)
        self.count = new_val

    def _check(self, number):
        if number < 0 or number > self.max_occupants:
             raise ValueError('Invalid count:{}'.format(number))
        return number

It errors out into:

Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    CorrectChair.load(1)
TypeError: load() missing 1 required positional argument: 
'number'

It appears to not be recognizing the self argument.. How can I fix this? Googling has not helped, every example I see makes it look like this should work.

It should be adding (number) to self.count, instead it ignores that its self referential, and asks for a 2nd argument.

  • 3
    You are not instantiating your class. You seem to be doing this -> `CorrectChair.load(1)`, when in fact you want to do something like `CorrectChair(1).load(1)`. Note the parenthesis after `CorrectChair` – idjaw Jul 09 '18 at 03:12
  • I suggest first holding your instance in a variable, then making your calls -> `correct_chair = CorrectChair(1)`. Then you can make your instance method calls -> `correct_chair.load(1)` – idjaw Jul 09 '18 at 03:13

3 Answers3

0

You must create an instance and call the methods on it:

replace CorrectChair.load(1) with:

c_chair = CorrectChair(some_id)
c_chair.load(1)
billjamesdev
  • 14,554
  • 6
  • 53
  • 76
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0

The load function is actually a object method. In the Python world, the first parameter of a object method always points to the instance, which will implicitly passes to the method before invoking. To call a object method, you first need to create a object, then invoke that method by the dot syntax. Autually

e.g

id = 3
newCorrectChair = CorrectChair(id)

# self is implicitly passed here, this style stems from C.
CorrectChair(id).load(10) 

If you was trying to write a class method, you have to add a @classmethod decorator.

class CorrectChair:
    # Blah...
    @classmethod
    def load(cls, num):
        # do something
        return

If you was trying to write a static function, you should decorate that method with @staticmethod decorator.

class CorrectChair:
    # Blah...
    @staticmethod
    def load(cls, num):
        # do something
        return
Stephen.W
  • 1,649
  • 13
  • 14
0

The error is showing that you're trying to call a method straight from the class, while the method expects an object reference as well. Before you make any call for any of those methods that include 'self', you need to create an instance of that class first

In your case, the code should be:

correct_chair = CorrectChair(id)
correct_chair.load(1)

Comparing to the method in your class - correct_chair corresponds to self, and 1 corresponds to 'number' in the method

def load(self, number):
    new_val = self.check(self.count + number)
    self.count = new_val
Ben
  • 3
  • 2