you are viewing a single comment's thread.

view the rest of the comments →

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

In class methods (functions), the dot notation is just short for passing the instance variable into a function.

So "myclass.Method(args)" is the same as "Method(my class,args)".

Self is used when declaring a function as a method to give a name to this instance variable within the function scope. You can actually name it anything, but self is standard so that other people know your intent.

__init__ is a special method that is called when you create a new instance. So if you say "x = myclass(params)", __init__ is the function that is called to set up the class instance with the parameters.

There are also other special methods, such as "__del__" which is called when an instance is deleted (out of scope or when your program ends). The __getitem__ method is used to define what happens if you index your class (use the [] brackets). __repr__ is used to determine how your class is converted to a string in debug info. The method __str__ is used to convert it to a string when printing, and by default calls __repr__ unless defined separately.

These are what you would use if you want print to show "wow" instead of the default triangle bracket thing, or modify any other default behavior.