10

everything was going great, I was loving it,

then self happens

why should one pass self (current object refrance) to the constructor in python , why doesn't this keyword exist in python.

coming from java I always forget to add it.

just WHY?????

Comments
  • 5
    Well I use pycharm which automatically adds "self" as an argument so never faced this problem.

    PyCharm is the best python IDE and I highly reccomend it.
  • 3
    This happened to me all the time when I first started Python.

    This page explains it nicely. It makes sense to me even more after reading:

    https://programiz.com/article/...

    Will probably still be weird for programmers of other OOP languages for a while.
  • 0
    @harambae ooh so that's why everybody uses it everywhare on the internet
  • 1
    @rammmukul well ya, PyCharm has a lot of nice features. Its a full fledged IDE and not a light weight code editor, so keep that in mind.
  • 0
    @corscheid thanks for sharing that , was good read , seems like its not necessary just as i thought but staying anyway .
  • 1
    @harambae yeah it's better than Geany and SPE and actually works, unlike Eclipse+PyDev.
  • 1
    Ooh and not just to constructor (__init__)
    have to pass it to every method ,this is so unintuitive for me.
  • 2
    As a person who codes mostly just in PY, this is something normal lol. BTW, I really like Spyder as IDE.
  • 2
    I think it makes sense. Self can't be some global "thing". It's depends on the context. So Python takes advantage of that and shows that it is a parameter
  • 1
    @kyleperik yeah, something like that.
  • 1
    @kyleperik self is not a keyword in python anything you pass as first parameter becomes reference for current object ,I think it is used to differentiate between object and class variable (self.x==object variable & x==class variable). It's necessary as unlike java there is no private and static keywords.The only problem is that you have to pass it every method definition and omit it while calling the method.
Add Comment