What is super().__init()

Started by
10 comments, last by Hraefn 9 years, 8 months ago

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

Advertisement

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

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/

Currently working on a scene editor for ORX (http://orx-project.org), using kivy (http://kivy.org).

super(Player, self) says 'give me an object representing the parent class of self', where 'self' is python convention for the current instance. .__init__(...) 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.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

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'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

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.

super(Player, self) says 'give me an object representing the parent class of self', where 'self' is python convention for the current instance. .__init__(...) 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

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! :)

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.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

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 :)

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

This topic is closed to new replies.

Advertisement