Helpful Information
 
 
Category: Python
understanding python

I have been browsing through some tuts and I have a few questions I was hoping someone could help me with.
in "def __init__(self, width=640,height=480):"
what is "self" for?
why is "init" surrounded by "_"
whole thing if needed is

class PyManMain:
"""The Main PyMan Class - This class handles the main
initialization and creating of the Game."""

def __init__(self, width=640,height=480):
"""Initialize"""
"""Initialize PyGame"""
pygame.init()
"""Set the window Size"""
self.width = width
self.height = height
"""Create the Screen"""
self.screen = pygame.display.set_mode((self.width
, self.height))

What does self.height do? why do can't you just leave it as width.
also I see "if __name__ == "__main__":" with name and main not being in the program. Is that a special in any way?


Thanks,

joe

Here it is!

The init method is a class constructor! So learn OOP programming and everything should work out!

what is "self" for?

"self" in Python classes refer to the object itself. It is similar to the self in C++ and "this" reference in Java/C#



why is "init" surrounded by "_"

the double underscores just indicate that init is a special method. There are also special methods like __doc__, __name__, __eq__ and many others.



What does self.height do? why do can't you just leave it as width.

when object is instantiated, height is assigned to it.



also I see "if __name__ == "__main__":" with name and main not being in the program. Is that a special in any way?

read here (http://www.artima.com/weblogs/viewpost.jsp?thread=4829)

dude, you should really get down to the basics before even trying pygame. that's just my $0.02. Please learn what you can from the Python tutorial/docs (http://docs.python.org/tut/tut.html)

Thank you for your help, and sorry for the wait, I went to Disney world.

I have tried the very basics of python, and have done some research on OOP.
But it seems that all I can do is simple math problems using only the interactive shell(ex ask for numbers and find area of square). I am just having a type of programing block, I don't know how to use anything to go beyond math, or even a program with any use that is even feasible for me to create with my limited knowledge. I have gone through many tutorials, and I know many simple basics of python, such as if, while, and try statements. I also can make dicts, lists, etc(I'm still working on classes, and specific details of functions such as what i specified above). But I have no clue how to put them to any practical use. I am going to pygame to simply do something else other than simple math. So if you could, would you please help my decide on something that I can do with my small amount of knowledge, that would also help me learn python better as well. I don't do programming for school or for work, so I am never challenged to do a project, and have a hard time finding one that i could make.


Thanks for the help,


Joe

Thank you for your help, and sorry for the wait, I went to Disney world.

I have tried the very basics of python, and have done some research on OOP.
But it seems that all I can do is simple math problems using only the interactive shell(ex ask for numbers and find area of square). I am just having a type of programing block, I don't know how to use anything to go beyond math, or even a program with any use that is even feasible for me to create with my limited knowledge. I have gone through many tutorials, and I know many simple basics of python, such as if, while, and try statements. I also can make dicts, lists, etc(I'm still working on classes, and specific details of functions such as what i specified above). But I have no clue how to put them to any practical use. I am going to pygame to simply do something else other than simple math. So if you could, would you please help my decide on something that I can do with my small amount of knowledge, that would also help me learn python better as well. I don't do programming for school or for work, so I am never challenged to do a project, and have a hard time finding one that i could make.


Thanks for the help,


Joe

one of the ways you can improve on your Python knowledge is to join some Python forums, like this (http://python-forum.org/py/index.php). you can try to solve other people's problems, at the same time, see how other people solve those problems and learn from them. Slowly, you will become a proficient.
However, another way is to get a good book with exercises, like the Dietel series How to Program in Python. There are exercises at the end of each chapter so you can try to do them. Just some suggestions.

"self" in Python classes refer to the object itself. It is similar to the self in C++You would generally only use self in C++ when you're dealing with static members, otherwise you would use this which refers to the object instance explicitly.

@OP, here's another eg with self
With C++,


class foo {
static int y = 11;
int q;
...
}


y is shared by all instances of foo due to static keyword and q is an instance variable that is unique to each instance of foo.
This is how it looks like in Python:


class foo:
y = 11
def __init__(self):
self.q = 99










privacy (GDPR)