Jump to content
  • Advertisement
Sign in to follow this  
HeyHoHey

python general classes question

This topic is 4024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

okay im just learning about classes in the book im reading on python. i was just wondering can someone help explain to me the reasons for having to use def __init__ as the first method in every class? also what is up with self? sorry if u dont understand what self is i mean the first paramater(my book said he uses self always others use other things). why do i have to do def __init__(self, other params, params): or def getName(self):. i just dont get the point of self or init. thanks hey ho hey

Share this post


Link to post
Share on other sites
Advertisement
__init__ is the equivalent of a constructor in python. It's basically a special function called upon an object's creation, where you put your aptly named initialization code for that class. Note that defining an __init__ isn't required unless you're actually going to well, use it.

The "self" issue is kind of a weird syntax thing. If you've worked with a language like C/C++ or Java before, "self" is the equivalent of the "this" pointer/reference. You note that a class's member functions need "self" as the first parameter. This is more or less the same thing with the other languages just mentioned, just that the "this" pointer passing is implicit. "self" is just a way to refer to your object itself.

Share this post


Link to post
Share on other sites
x = SomeClassX() calls SomeClassX.__init__(x) if __init__ exists.

x.some_method() calls SomeClassX.some_method(x) - x is the parameter identified as self.

x.some_other_method(param1, param2) calls SomeClassX.some_other_method(x, param1, param2).

That's it in a nutshell. Now, if you want a discussion of why it's that way...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!