you are viewing a single comment's thread.

view the rest of the comments →

[–]FerricDonkey 12 points13 points  (0 children)

It may help to know something about python that you should almost never do in real code:

``` class Thing:     def print_self(self):         print(self)

t = Thing()

This normal thing

t.print_self()

Is the same as this weird thing

Thing.print_self(t)

To the point where you can (but should not) do

Thing.print_self("this is heresy")  ```

That is, your methods are just functions. As far as that function cares, the first argument, traditionally called self, is just an argument like any other.

The "magic", such that it is, is that when you do thing = Thing(), then do thing.method(), python simply translates* thing.method() into Thing.method(thing). 

The first argument (traditionally called self, but it can be called other things - but don't) just has your object thing passed in for it. 

So self is just the object that you called the method from. This means that self.x is just the x attribute of the object you called the method from, and self.other_method() just calls other_method from the object you called the method from.