-1

I have created an object an included a function with 3 parameters, however, the program won't pick up my last parameter dy.

The code

class blaster(object):

    def __init__(self,x,y,r):
        self.x = x
        self.y = y
        self.radius = r
        self.xvelocity = 5
        self.yvelocity = 0
        self.blastercount = 0
        self.isblasting = False
        self.xarray = []
        self.yarray = []
        self.xcounter = []
        self.ycounter = []
    def initiate_blast(self,xx,yy,dx,dy):
        self.xarray.append(int(mn.x))
        self.yarray.append(int(mn.y))
        self.xcounter.append(int(dx))
        self.ycounter.append(int(dy))

The function gets called:

blaster.initiate_blast(troop.x,troop.y,5,5)

The output:

TypeError: initiate_blast() missing 1 required positional argument: 'dy'
  • You probably redefined *initiate\_blast* below. – CristiFati May 28 '20 at 05:58
  • 1
    Does this answer your question? [TypeError: Missing 1 required positional argument: 'self'](https://stackoverflow.com/questions/17534345/typeerror-missing-1-required-positional-argument-self) – Masklinn May 28 '20 at 06:03

1 Answers1

2

initiate_blast is defined as an instance method, and you're calling it on the class.

Therefore there's no implicit self parameter being passed in, so you're calling this:

blaster.initiate_blast(self=troop.x, xx=troop.y, yy=5, dx=5)

Even if the call itself didn't blow up, it'd explode on the very first line as self would be nonsense without an xarray attribute.

It's extremely bad form to name "proper" classes in lowercase incidentally. It's OK for some builtins and class-based decorators or the like, but that's not the case here, blaster is obviously intended as an actual type, not as an arbitrary callable which just happens to be a class.

Masklinn
  • 34,759
  • 3
  • 38
  • 57