Classes, "self".. but why?


#1



Hi, thanks for this course, overall very good. Maybe I should wait until I gain more experiences with "classes" but just wondering the following:

"The first argument init() gets is used to refer to the instance object, and by convention, that argument is called self."

I understand it first needs to call itself to determine where to add the new arguments to. BUT, if the convention is to always to use "self", isn't this terribly redundant? Or can you give examples where you would not use self? From the rookie perspective, this looks an awfully longwinded way to express zebra.name , but i guess the insight will come later :slight_smile:


Replace this line with your code.


#2

You call functions/methods/other callables, unless your object is callable, then you won't call it.

Why is it redundant, what is redundant? Where would that information already be available?


#3

i meant if we're always supposed to call init(self, x,y) why not just call it self_init(x,y).


#4

__init__ is called when an object is created, if you create a method with a different name, then that method won't be called when an object is created, because it isn't named __init__

If you mean that Python should be able to call a different method on object creation, with a name of your choosing, then how would it know which one? And why should the name change if it's always got the same purpose, isn't it better that it's always named the same thing?

Removing parameters would be a bad idea, because if a method doesn't have access to the values it needs to operate on, then how is it supposed to do its job?


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.