"self" not defined

Recommended Posts

Hi everyone I'm trying to make my first game using pygame and I was wondering if anyone could help me. I get an error in Main() saying that "classes.classprotagonist.motion()" needs argument 'self'. When I comply with this it gives another error saying 'self' is not defined. I'm stuck since

Not sure how much you understand of classes, but "classes.classprotagonist" is a class. It's like a template, a sort of empty form with checkmarks and dotted lines, so you can check-off common things, and write lines of text at the indicated places.

In particular, the class is not "live", it contains no data like a filled-in form. To make a "live" version of the template you make a xerox-copy of it, and you it hand over to the person that should fill it with checkmarks and lines of text. In Programming, it's called "instantiating a class". The result is called "an object". The object has the same shape as the class, but it's meant for filling with data.

You instantiate the "classes.classprotagonist" class in the first line that I quoted, giving it values that match with the __init__ function in the class.

The created object is assigned to the "protagonist" variable. The name "self" in the class refers to such an object, ie it points to the object that stores the actual data.

The "classes.classprotagonist.motion()" calls the function "motion" in the class. If you compare the class definition with how you call it, you see it's missing a 'self' argument. The template says you need one, and in the call you provide none. This is why Python refuses to perform the call, it's missing the object with data.

There are two ways out of this, a nice theoretical one, and a much more practical one. First the theoretical one:

classes.classprotagonist.motion(protagonist)

I copied your line, and add the object as argument. I supplied a "self" for the call, and now it will work.

The more practical one uses the fact that the "protagonist" is created from the class. It has everything that the class has, including the "motion" function. In other words, there is no need to go through "classes.classprotagonist", as "protagonist" already has the function. That leads to

protagonist.motion()

From the object, I call the "motion" function, and since I use the object to reach the function, the object inserts itself as first argument, so I don't need to supply it. Neat eh?

Not sure how much you understand of classes, but "classes.classprotagonist" is a class. It's like a template, a sort of empty form with checkmarks and dotted lines, so you can check-off common things, and write lines of text at the indicated places.

In particular, the class is not "live", it contains no data like a filled-in form. To make a "live" version of the template you make a xerox-copy of it, and you it hand over to the person that should fill it with checkmarks and lines of text. In Programming, it's called "instantiating a class". The result is called "an object". The object has the same shape as the class, but it's meant for filling with data.

You instantiate the "classes.classprotagonist" class in the first line that I quoted, giving it values that match with the __init__ function in the class.

The created object is assigned to the "protagonist" variable. The name "self" in the class refers to such an object, ie it points to the object that stores the actual data.

The "classes.classprotagonist.motion()" calls the function "motion" in the class. If you compare the class definition with how you call it, you see it's missing a 'self' argument. The template says you need one, and in the call you provide none. This is why Python refuses to perform the call, it's missing the object with data.

There are two ways out of this, a nice theoretical one, and a much more practical one. First the theoretical one:

classes.classprotagonist.motion(protagonist)

I copied your line, and add the object as argument. I supplied a "self" for the call, and now it will work.

The more practical one uses the fact that the "protagonist" is created from the class. It has everything that the class has, including the "motion" function. In other words, there is no need to go through "classes.classprotagonist", as "protagonist" already has the function. That leads to

protagonist.motion()

From the object, I call the "motion" function, and since I use the object to reach the function, the object inserts itself as first argument, so I don't need to supply it. Neat eh?learnedmorewi

omg I learned more with your reply than all the hours I spent learning objects and classes...THANKSS!