Jump to content

  • Log In with Google      Sign In   
  • Create Account

What is super().__init()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 26 July 2014 - 04:56 AM

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



Sponsor:

#2 Lysy   Members   -  Reputation: 588

Like
0Likes
Like

Posted 26 July 2014 - 08:44 AM

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



#3 KnolanCross   Members   -  Reputation: 1362

Like
3Likes
Like

Posted 26 July 2014 - 10:11 AM

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


#4 swiftcoder   Senior Moderators   -  Reputation: 10367

Like
2Likes
Like

Posted 26 July 2014 - 01:17 PM

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 - Software Engineer @Amazon - [swiftcoding]


#5 superman3275   Crossbones+   -  Reputation: 2061

Like
1Likes
Like

Posted 26 July 2014 - 06:22 PM

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 ph34r.png!

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 smile.png!


#6 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 27 July 2014 - 07:37 AM

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 



#7 Lysy   Members   -  Reputation: 588

Like
1Likes
Like

Posted 27 July 2014 - 10:30 AM

 

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



#8 superman3275   Crossbones+   -  Reputation: 2061

Like
1Likes
Like

Posted 27 July 2014 - 11:41 AM

 

 

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 ph34r.png!

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 smile.png!


#9 Lysy   Members   -  Reputation: 588

Like
0Likes
Like

Posted 27 July 2014 - 01:28 PM

 

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



#10 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 28 July 2014 - 12:10 AM

 

 

 

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, 28 July 2014 - 12:16 AM.


#11 superman3275   Crossbones+   -  Reputation: 2061

Like
0Likes
Like

Posted 28 July 2014 - 07:10 AM

 

 

 

 

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.


I'm a game programmer and computer science ninja ph34r.png!

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 smile.png!


#12 Kurai Tsubasa   Members   -  Reputation: 207

Like
0Likes
Like

Posted 29 July 2014 - 06:34 AM

 

 

 

 

 

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS