What is super().__init()

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

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 !

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 !

Advertisement

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

This topic is closed to new replies.

Advertisement