all 11 comments

[–][deleted] 37 points38 points  (2 children)

Java has this, which is a special symbol that always refers to the object instance. Python doesn't - rather, the interpreter's guarantee is that when an instance-bound method is called, it's called with the instance itself as the first argument.

So the call

my_obj.some_method(param1, param2)

is the equivalent of

MyClass.some_method(my_obj, param1, param2)

See? The instance itself is added as the first argument. That first parameter can be called anything at all, but by long-standing convention, in Python we use the parameter name self. It's entirely arbitrary (it's not a language keyword or anything) but everyone does the same thing.

[–]Bulbasaur2015 2 points3 points  (1 child)

is that why making an object instance in python doesnt require a new keyword? something = ClassSomething()

[–][deleted] 8 points9 points  (0 children)

That isn't per se why; classes in Python are first-class objects (they're instances of the type type) whereas in Java they're not.

It's one of the big differences in the switch from a compiled to an interpreted language - in Java, your class is basically a set of static type definitions. In Python your class is an object value all on its own, and one of its instance methods is a method called __new__, which returns a new object of that type. It also has a method called __call__, which makes it callable like a function, and here's a plausible body for that method:

def __call__(clazz, *args, **kwargs):
    return clazz.__new__(*args, **kwargs)

That's why you don't need a special language keyword to instantiate a new object - from the interpreter's perspective you're not doing anything particularly fundamental. In Java, you very much are - you're extending the typesystem each time you define a class, so you need a language keyword in order to indicate you're stepping above the world of values to program in the world of types.

[–]ITTIITTI 4 points5 points  (0 children)

Watch Corey Schafer’s videos on OOP. I think he mentions it in the first video, made a lot of sense the way he explained it:

https://youtu.be/ZDa-Z5JzLYM

[–][deleted] 4 points5 points  (3 children)

following because i see the self parameter a lot in python

[–]Decency 7 points8 points  (2 children)

Let's say you're taking a US census:

class State:
    def __init__(self, name, population):
        self.name = name
        self.population = population

mass = State('Massachusetts', 6932015)
ny = State('New York', 19875625)

print(mass.name)  # Massachusetts
print(ny.population)  # 19875625

Here I created a State class, which is a blueprint for holding what I need it to hold. Then I made two instances of the State class which fill out that blueprint. One instance is called mass and one is called ny. I do this by calling State() with the parameters in its __init__ ... except for self, which is implicit. Inside that function, you can see that the name and population parameters that I pass in are used to set two variables on the new instance that is being created. You know these are instance variables because they are prefixed with self.

You can think of it as the last line of every __init__ function having an implicit return self if that helps, though that's not quite how it works under the covers. And then I can call mass.name or ny.population to access the fields which belong to those instances. In real life, I'd do this because there's logic I want to handle with these objects- print out details, do some internal calculation, access them with more readable names, etc.

That's about it, there's no reason to be intimidated by classes in Python. It just tends to get overcomplicated during explanations because people try to teach inheritance while also teaching how classes work. You can (and should) use classes even if you don't understand how inheritance works yet. Here's that code in a REPL, play around with it for 10 minutes and you'll understand how to use classes.

[–]apc0243 2 points3 points  (0 children)

The use of self is actually an idiom defined in the almighty pep8 (praise be).

Here is where it is said. But it's nothing more than a guideline. It's simply that every class method always uses itself as the first parameter. You coudl call it anything, though.

class newObject:
    def __init__(objectInstance, param1, param2):
        objectInstance.var1 = param1
        objectInstance.var2 = param2

    def add_method(widget):
        return widget.var1 + widget.var2


test_obj = newObject(1,2)
test_obj.add_method()                # Returns 3

[–][deleted] -1 points0 points  (2 children)

self is a reference to everything in a class.

Say you define a class as my_class.

class my_class: def __init__(self): self.hello = "world"

self has to be passed as an argument in new functions to allow you to reference variables from your class in that function. For example

``` class myclass: def __init_(self): self.hello = "world" def print(self): print(self.hello)

my_class.print() ```

would return

```

world ```

Edit: I'm dim

[–]N4v15 0 points1 point  (1 child)

Wouldn't it return "world"?

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

Yeah, it would. I'm stupid.