1

When I try to run self.chunkIt inside this class Game, I receive this error:

Traceback (most recent call last):
  File "C:\Users\Araújo\Desktop\hq_player.py", line 128, in <module>
    g.new_question([1,2])
  File "C:\Users\Araújo\Desktop\hq_player.py", line 84, in new_question
    print(self.chunkIt(['1','2'],2))
TypeError: chunkIt() takes 2 positional arguments but 3 were given

If try to run it alone in console, It works just fine. Why is it passing 3 arguments? class Game: def init(self,number_questions,devices): self.number_questions = number_questions self.devices = devices self.question = 0

     def new_question(self,options):
         print(self.chunkIt(['1','2'],2))

      def chunkIt(seq, num):  
        avg = len(seq) / float(num)
        out = []
        last = 0.0

        while last < len(seq):
            out.append(seq[int(last):int(last + avg)])
            last += avg
        return out
hopieman
  • 399
  • 7
  • 22
  • 3
    `self` is the third arg because of how you're calling it. You should add a `self` param to `chunkIt` – pushkin Jun 27 '18 at 21:24
  • If `chunkIt` really has no need for `self`, you could make it a `@staticmethod`, or, probably better, just make it a top-level function instead of a method. But if `chunkIt` _might_ conceptually have some use for `self`, even though your current implementation doesn't use it, it makes more sense to add the `self` parameter and use it as a normal method. – abarnert Jun 27 '18 at 21:26

2 Answers2

4

self is the 3rd argument as I am assuming these methods are within a class given new_question calling self.

  def chunkIt(self, seq, num):  
    avg = len(seq) / float(num)
    out = []
    last = 0.0

    while last < len(seq):
        out.append(seq[int(last):int(last + avg)])
        last += avg
    return out

Regardless if you define it or not, unless you specify a method as static, it will always pass self to the method. Therefore by calling print(self.chunkIt(['1','2'],2)) it actually is being sent as print(self.chunkIt(self, ['1','2'],2))

pypalms
  • 461
  • 4
  • 12
1

You forgot to add self to chunkIt method:

 def new_question(self,options):
     print(self.chunkIt(['1','2'],2))

 def chunkIt(self, seq, num):  
     avg = len(seq) / float(num)
     out = []
     last = 0.0

     while last < len(seq):
         out.append(seq[int(last):int(last + avg)])
         last += avg
     return out
Artsiom Praneuski
  • 2,259
  • 16
  • 24