Thursday 12 July 2018

Python Class Instance Object

Cited from the book "Python How to Program"

All methods, including constructors, must specify at least one parameter. This parameter represents the object of the class for which the method is called. This parameter often is referred to as the class instance object. This term can be confusing, so we refer to the first argument of any method as the object reference argument, or simply the object reference. Methods must use the object reference to access attributes and other methods that belong to the class. By convention, the object reference argument is called self.

Failure to specify an object reference (usually called self) as the first parameter in a method definition causes fatal logic errors when the method is invoked at runtime.

Python inserts the first (object reference) argument into every method call, including a class’s constructor call. 


No comments:

Post a Comment