Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Location
    U.S.A
    Posts
    129
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Code:
    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
    Last edited by awsomejoe23; 06-16-2007 at 05:24 AM.

  • #2
    New Coder
    Join Date
    Jun 2006
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here it is!

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

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by awsomejoe23 View Post
    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

    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

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Location
    U.S.A
    Posts
    129
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by awsomejoe23 View Post
    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. 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.

  • #6
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by ghostdog74 View Post
    "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.
    Omnis mico antequam dominus Spookster!

  • #7
    New Coder
    Join Date
    May 2006
    Posts
    39
    Thanks
    0
    Thanked 1 Time in 1 Post
    @OP, here's another eg with self
    With C++,
    Code:
      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:
    Code:
      class foo:
          y = 11
          def __init__(self):
              self.q = 99


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •