Sign in to follow this  

What is super().__init()

This topic is 1267 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

Hi,i just found a TMX library but i can't find any tutorial that explain it clearly so i tried to understand the library myself but i don't know what is super().__init__().I already tried to search about it but i don't really understand it,the explaination is to advance for me sad.png  because i'm still new in programming world so this is code:

class Player(pygame.sprite.Sprite):
    def __init__(self, location, orientation, *groups):
        super(Player, self).__init__(*groups)
        self.image = pygame.image.load('sprites/player.png')
        self.imageDefault = self.image.copy()
        self.rect = pygame.Rect(location, (64,64))
        self.orient = orientation 
        self.holdTime = 0
        self.walking = False
        self.dx = 0
        self.step = 'rightFoot'
        # Set default orientation
        self.setSprite()
        

Any helps will be very appreciate and please explain it using easy example biggrin.png if it possible also if you need a full code i will add it since the code is too long.Thank you so much

Share this post


Link to post
Share on other sites

Well, you say you are new to programming. Do you know what does inheritance mean?

Share this post


Link to post
Share on other sites

Google for inheritance, it is a mechanic on Object Oriented programing that roughly saying makes one class (in your case, Player) have all the methods and private variables of the class that is its parent (in your case Sprite). super there is used to call the init method of the sprite class.

 

Here you can find a better explanation (not my blog): http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/

Share this post


Link to post
Share on other sites

[tt]super(Player, self)[/tt] says 'give me an object representing the parent class of self', where 'self' is python convention for the current instance. [tt].__init__(...)[/tt] is calling the constructor on that object, which will be the constructor defined in the parent class.

 

If that doesn't make sense, you'll need to read up on classes and inheritance.

Share this post


Link to post
Share on other sites

It runs the parent class' Constructor. More formally, it gives a version of the Player object which in the form of the Player objects parent class.

Share this post


Link to post
Share on other sites

Well, you say you are new to programming. Do you know what does inheritance mean?

Yes i do when learn about the 'basic' of python,but i never learn about class much,since the books i read never using class on their example.But after reading that books and look some example on internet i realize that i must learn about it much ^^

 

 

 

Google for inheritance, it is a mechanic on Object Oriented programing that roughly saying makes one class (in your case, Player) have all the methods and private variables of the class that is its parent (in your case Sprite). super there is used to call the init method of the sprite class.

 

Here you can find a better explanation (not my blog): http://www.jesshamrick.com/2011/05/18/an-introduction-to-classes-and-inheritance-in-python/

Thank you it help me understanding about class and super(There is a link in comment),but i still not get used with it,maybe playing around a little bit tomorrow will help me get used to it.

 

 

[tt]super(Player, self)[/tt] says 'give me an object representing the parent class of self', where 'self' is python convention for the current instance. [tt].__init__(...)[/tt] is calling the constructor on that object, which will be the constructor defined in the parent class.

 

If that doesn't make sense, you'll need to read up on classes and inheritance.

Well,i think i understand that a little.I will play around with it tomorrow so i can really understand it

 

 

It runs the parent class' Constructor. More formally, it gives a version of the Player object which in the form of the Player objects parent class.

I will try to play with it a bit to really understand that

 

 


Well,Thank you everyone to help me.I will try my best biggrin.png 

Share this post


Link to post
Share on other sites

 

Yes i do when learn about the 'basic' of python,but i never learn about class much,since the books i read never using class on their example.But after reading that books and look some example on internet i realize that i must learn about it much ^^

 

For a book, I can recommend 'Learning Python'. But there's also many internet courses, amongst them is http://learnpythonthehardway.org/ which I think is the most appreciated python course on this forum. I'm quite sure it covers basics of OOP and such. Good luck with your education! :)

Share this post


Link to post
Share on other sites

 

 

Yes i do when learn about the 'basic' of python,but i never learn about class much,since the books i read never using class on their example.But after reading that books and look some example on internet i realize that i must learn about it much ^^

 

For a book, I can recommend 'Learning Python'. But there's also many internet courses, amongst them is http://learnpythonthehardway.org/ which I think is the most appreciated python course on this forum. I'm quite sure it covers basics of OOP and such. Good luck with your education! smile.png

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

I recommend you just look at examples of programs which use classes, and try writing some OOP programs yourself. OOP is the kind of thing that doesn't really "click" until you start using it yourself.

Share this post


Link to post
Share on other sites

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

Yeah I could mention I didn't read it, only relied on the opinion I've read here that it is a good tutorial :)

Share this post


Link to post
Share on other sites

 

 

 

Yes i do when learn about the 'basic' of python,but i never learn about class much,since the books i read never using class on their example.But after reading that books and look some example on internet i realize that i must learn about it much ^^

 

For a book, I can recommend 'Learning Python'. But there's also many internet courses, amongst them is http://learnpythonthehardway.org/ which I think is the most appreciated python course on this forum. I'm quite sure it covers basics of OOP and such. Good luck with your education! smile.png

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

I recommend you just look at examples of programs which use classes, and try writing some OOP programs yourself. OOP is the kind of thing that doesn't really "click" until you start using it yourself.

 

 

 

 

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

Yeah I could mention I didn't read it, only relied on the opinion I've read here that it is a good tutorial smile.png

 

 

Thank you for the Tutorial actually i already do that but never finished it,but i do learn class from video tutorial and book.So I tried to using the super()__init__() and i figured it what it is for smile.png  but i still curious why we need to pass  *groups inside __init__() in my code when i play around with super i never pass anything inside __init__() because i don't know what it is for(i only know def __init__ or something to init first).This is my last question biggrin.png .Thank you very much

Edited by Kurai Tsubasa

Share this post


Link to post
Share on other sites

 

 

 

 

Yes i do when learn about the 'basic' of python,but i never learn about class much,since the books i read never using class on their example.But after reading that books and look some example on internet i realize that i must learn about it much ^^

 

For a book, I can recommend 'Learning Python'. But there's also many internet courses, amongst them is http://learnpythonthehardway.org/ which I think is the most appreciated python course on this forum. I'm quite sure it covers basics of OOP and such. Good luck with your education! smile.png

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

I recommend you just look at examples of programs which use classes, and try writing some OOP programs yourself. OOP is the kind of thing that doesn't really "click" until you start using it yourself.

 

 

 

 

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

Yeah I could mention I didn't read it, only relied on the opinion I've read here that it is a good tutorial smile.png

 

 

Thank you for the Tutorial actually i already do that but never finished it,but i do learn class from video tutorial and book.So I tried to using the super()__init__() and i figured it what it is for smile.png  but i still curious why we need to pass  *groups inside __init__() in my code when i play around with super i never pass anything inside __init__() because i don't know what it is for(i only know def __init__ or something to init first).This is my last question biggrin.png .Thank you very much

 

__init__ is called a Constructor. It's a function (like any other), with a small twist. It gets called every time you create a new instance of a Class. For example, let's say we want to create a generic enemy class. We want all of the instances of our class to be the same in every regard except their health. A class like that would look something like this:

class Enemy(object):
    def __init__(self, health):
        print "Inside __init__"
        self.health = health

Every time we create a new Enemy, __init__ gets ran with whatever "health" we pass in. Let's create some Enemy objects:

# You pass in what their health should be when creating them
enemy_one = Enemy(10)
enemy_two = Enemy(5)

And here's the program output:

Inside __init__
Inside __init__

When you make a new Object and pass things to it in parenthesis, you're actually passing stuff to the __init__ function.

Share this post


Link to post
Share on other sites

 

 

 

 

 

Yes i do when learn about the 'basic' of python,but i never learn about class much,since the books i read never using class on their example.But after reading that books and look some example on internet i realize that i must learn about it much ^^

 

For a book, I can recommend 'Learning Python'. But there's also many internet courses, amongst them is http://learnpythonthehardway.org/ which I think is the most appreciated python course on this forum. I'm quite sure it covers basics of OOP and such. Good luck with your education! smile.png

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

I recommend you just look at examples of programs which use classes, and try writing some OOP programs yourself. OOP is the kind of thing that doesn't really "click" until you start using it yourself.

 

 

 

 

 

In the Python world, LPTHW is great for everything but OOP. It's unanimously agreed that the OOP section in that book is its major downfall.

 

Yeah I could mention I didn't read it, only relied on the opinion I've read here that it is a good tutorial smile.png

 

 

Thank you for the Tutorial actually i already do that but never finished it,but i do learn class from video tutorial and book.So I tried to using the super()__init__() and i figured it what it is for smile.png  but i still curious why we need to pass  *groups inside __init__() in my code when i play around with super i never pass anything inside __init__() because i don't know what it is for(i only know def __init__ or something to init first).This is my last question biggrin.png .Thank you very much

 

__init__ is called a Constructor. It's a function (like any other), with a small twist. It gets called every time you create a new instance of a Class. For example, let's say we want to create a generic enemy class. We want all of the instances of our class to be the same in every regard except their health. A class like that would look something like this:



class Enemy(object):
    def __init__(self, health):
        print "Inside __init__"
        self.health = health

Every time we create a new Enemy, __init__ gets ran with whatever "health" we pass in. Let's create some Enemy objects:



# You pass in what their health should be when creating them
enemy_one = Enemy(10)
enemy_two = Enemy(5)

And here's the program output:



Inside __init__
Inside __init__

When you make a new Object and pass things to it in parenthesis, you're actually passing stuff to the __init__ function.

 

 

Ok,i got it now.Thank you very much for the explanationbiggrin.png

Share this post


Link to post
Share on other sites

This topic is 1267 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this