2

What is the dunder init (__init__) doing in my Python classes?

Comments
  • 4
    Every time you call MyStuff() to create an instance of your class, __init__ is called to initialize the instance. You can make __init__ take parameters beyond self and assign them to your instance.
  • 3
    @Sheep we need more people like you on StackOverflow xD
  • 0
    Maybe I'll try getting into it again, then.
  • 0
    @Sheep can you give me an example where __init__ takes parameters betond self?
  • 0
    I got one already. Thanks anyways. :)
  • 1
    Sorry, I hadn't gotten a notification for your comment. I learned using Learning Python from O'Reilly. It was extremely helpful. It can be dry at times, but it's a very informative read. After that, learn GUI programming, I recommend Qt using PyQt as it's fairly pervasive in the Python community. As far as I've seen, nothing will teach you the Object Oriented Model quite like a graphical framework.
  • 0
    @sheeponmeth thanks. OOP is running me confused. I currently learning things from Zed Shaw's LPTHW. I totally confused.
  • 0
    The basics of OOP are pretty straightforward, you just need it explained in a way that makes sense.

    Everything is an object, objects can also have objects as attributes (basically a variable). Some objects need data when they're initialized, like is that 'person' object male or female, how old, what colour eyes? Eyes could be an object of their own.

    Objects also have their own functions, they're typically called methods in OOP. That method belongs to that object. Objects can inherit both methods and attributes. Subclassing an object let's you take an existing object and fork it off into your own and customize it and make it more useful for your needs.

    At its core, an object is really like a package of variables and functions. When you subclass an object, you're really just packaging that object inside your own.
Add Comment