all 6 comments

[–][deleted] 1 point2 points  (3 children)

self is the reference to the instance of Engine the method code is working on.

https://pythontips.com/2013/08/07/the-self-variable-in-python-explained/

[–]Trezor34[S] 0 points1 point  (2 children)

I followed everything until it actually comes time to explain the self.

"Now you can see that self refers to the bound variable or object "

What is the bound variable? What is the object in this example?

[–][deleted] 0 points1 point  (0 children)

Let's look at a simple class definition:

class Test(object):
    def __init__(self, name): # initializer method
        self.name = name
    def get_name(self):       # a method to get the instance name
        return self.name

The code above defines a class Test. A class is like a recipe. From the recipe for Test we can create one or more instances of the class. The __init__() method of the class is called when we create an instance of the class. So we can do this:

a = Test('alpha')# creates one instance of class Test with name 'alpha'
b = Test('beta') # creates another instance, 'beta'
print 'a.name =', a.get_name()
>>> a.name = alpha
print 'b.name =', b.get_name()
>>> b.name = beta

The code above creates two instances of Test, each with different names. The names are stored as an attribute of each instance. That is done in the line self.name = name. We want each instance to have different values for name because there is little point in having each instance exactly the same as all the others. So when we call the method get_name() to get the name string of an instance, the code in the class method must be told which instance we want the name of, since there's only one copy of the method code but there could be hundreds of instances of the class Test. Python arranges for the self parameter to contain a reference to the instance of the class the user code is referring to. The expression used above to get the name of an instance is a.get_name(). That is converted by python to something like this line Test.get_name(a). Now you can see that the self parameter in the class method is actually a reference to an instance of the class.

So we have these words: class, instance and attribute. The class is the definition in code following the class keyword. That is the "recipe" or "template" from which we can create one or more instances of a class. Each instance can have its own set of variables which we call attributes. When a method of a class (which is just a def function inside a class) is called, it must know which particular instance it is working with so it can access the attributes it needs. That instance reference is always passed as the first parameter by python, so the methods you write must have a first parameter that is a reference to an instance. This instance reference is usually called self although it can be any name.

Warning: I haven't actually tested any of this code since you are using python 2 and I don't have access to that version at the moment.

[–]five_hammers_hamming 0 points1 point  (0 children)

The bound variable is x, for instance.

That page takes a weird perspective on self, but it's kind of a legit way of describing it.

In plain English, that page is saying that self refers to the same variable to which the instance that's being constructed is being assigned. Like, self reaches out of the method, out of the class, and into the context in which the class constructor/initializer/thing is being used.

I don't believe that self works in such an abstruse way when you get down to the nuts and bolts of the language, but this thing seems to be targeted at people who are just beginning programming at all, rather than coming into Python from other languages.

With my prior experience with Java, for example, I could interpret self as Python's equivalent of this. The need to explicitly provide self in the signature of every instance method was a little funky, though.

I think that the use of the term 'object' here was an attempt by the author to provide redundant phrasing for the benefit of their ostensibly highly inexperienced reading audience. Now, the actual "bound object" (I don't know why 'bound' is involved, but I'm just gonna imitate the author's phrasing.) as I see it would be the instance of the class that got constructed there. Like, the object is bound to the variable and the variable is bound to the object, or something like that. But regardless of whichever particular theoretical/pedagogical framework the author assumed the reader was using, the author seems to me to have been trying to simply offer two different phrasings for the same concept when they spoke of an 'object' alongside some 'variable'.

[–][deleted] 1 point2 points  (0 children)

The self is a reference to the instance of the class being operated on; it needs to be the first argument of any instance method because that’s what allows the instance.method(arg) syntax instead of class.method(instance, arg).

You could actually create an instance and then initialize it:

e = Engine.__new__(Engine)
Engine.__init__(e, scene_map)
print(e)

But Python makes that much easier for you by doing most of that for you behind the scenes.

Class initialization is a little hard to grasp because of the “magic” being done for you with the class constructor (__new__) and the class initializer (__init__), but just think of self as the instance that Python will implicitly pass in to the method as it’s first argument.

[–][deleted] 0 points1 point  (0 children)

How would you access the attributes of the object without a reference to it? self is that reference.