0

I've recently learned about class, method, and (self) functions. While I seem to understand the syntax side of things, I find having to type [self] in front of variables bothersome with very little benefit. In what scenario would this be beneficial compared to simply using individual functions without class reference?

  • 2
    Classes are one of the core concepts of object oriented programming. It's a good idea to use them. And `self` references the current instance of the object. Please also see https://en.wikipedia.org/wiki/Object-oriented_programming for a general idea. – Freddy Mar 11 '16 at 23:39
  • Please refer to this question: http://stackoverflow.com/questions/7721920/when-do-you-use-self-in-python – Juan Pablo Mar 11 '16 at 23:44
  • Sure, the class syntax can be bothersome, and it may not give any benefit in a small program. But when things get complicated OOP is an excellent way to keep things organised, both during initial development and when you want to expand the program. You may find this article helpful: [Why is Object-Oriented Programming Useful?](http://inventwithpython.com/blog/2014/12/02/why-is-object-oriented-programming-useful-with-an-role-playing-game-example). – PM 2Ring Mar 12 '16 at 00:01
  • In other OOP languages, there is a `this` variable implicitly defined in methods. In pyhton, it is not implicitly created, and its name must not be `self` (although people use `self` by convention). – Sci Prog Mar 12 '16 at 00:04
  • @ColonelThirtyTwo: I don't think that's a suitable dupe target. The OP is aware of the syntax. They're asking _why_ they should go through the bother of using OOP. – PM 2Ring Mar 12 '16 at 00:15
  • I agree with @PM_2Ring, even though I just voted to close it down. First because his question is _mainly_ about the syntax and the fact that `self` feels *bothersome*, and because @Thomas_Wouters answer is just brilliant and deserves the 300+ votes ☺; and secondly, because the OP is actually lacking basic knowledge of OOP, and even programming. – zmo Mar 12 '16 at 00:19
  • Here's a simple example from core Python. When you call the built-in `str` function on any Python object that translates into a call of the object's `__str__` method (or its `__repr__` method if it doesn't have `__str__`). So each class can internally handle the details of creating a string representation of itself. Without that, you'd need to have explicit functions for each object type, eg `str_int`, `str_float`, `str_list`, `str_dict`, etc. Or `str` would have to be a giant function that handles every known object type, and if you added new types to the language you'd have to modify `str`. – PM 2Ring Mar 12 '16 at 00:21

1 Answers1

2

In what scenario would this be beneficial compared to simply using individual functions without class reference?

whoa… what a question! I believe you should start with a lecture on object oriented programing.

Simply said: the benefits of using a class over laying out functions in a module, is to enable many features of object oriented programing (encapsulation of data through a behaviour, class inheritance, properties…).

And in more length: the main idea behind OOP, is that you create a public programing interface, that other developers (including you) will use, and you can hide the inner workings, so once the public interface is all implemented, you don't care how things work internally, and can even change without breaking the code that use that.

So in this case, you create a class, and you implement your own algorithms on your own data, but all what people really care is how to change your data using those algorithms.

And those algorithms are being called methods and the data members.

In many languages, when you create a method (i.e. a function that is bound to an "object"), the reference to the current instance of the object is implicit (and might be optionally explicit in Java or C++, which is the this variable).

In python, the language designers chose for it to be explicit, and called by convention self. Then in rare cases, you can choose to call it this if you want (which can be useful when doing nested classes).

Finally, I talked about "classes", "instances" and "objects". An object is an instance of a class. What that means is that the class is here to lay out how things work (what are the members, what are the methods…). Then you do instanciate your object by calling the constructor. So then, you can have plenty of objects that share the same class. Which means they work similarly, but they have different data.

But here I only scratch the surface, and only want to show you that there are a lot of concepts behind the self and the classes.

For more on the topic, go read books and take programming classes:

zmo
  • 24,463
  • 4
  • 54
  • 90