Python Classes

Terry Reedy tjreedy at udel.edu
Mon Aug 4 19:26:20 EDT 2014


On 8/4/2014 6:44 PM, John Gordon wrote:

> __init__() is the initializer method, which is called as one step of
> creating a class object.

In fact, it is the last step and usually is the main step for 
user-defined classes, and the only step one need be concerned with.

> Object is the lowest-level class.  All other classes inherit from Object.

The spelling is 'object', with lowercase 'o'.  'Object' would have been 
less confusing, but all other builtin classes, are lowercase (some 
because they started as functions in Python 1.0 or soon thereafter).

> Within a class, self is a reference to the current class instance.

This is only true within a method definition and only when 'self' is 
given as the first parameter name.

class C:
   def meth_standard(self, other): pass
   # 'self' is an object of class C, 'other' to any other object.
   # Using 'self' is not required, but is the standard convention.

   def meth_brief(s, o): pass
   # 's' refers to an instance of class C, 'o' to any other object
   # ok for quick interactive use that one keeps private

   def meth_obnoxious(other, self): pass
   # 'other' is an instance of C, 'self' is any object
   # Anyone who publishes such code, except to illustrate trollish
   # behavior, is fishing for heated responses.

-- 
Terry Jan Reedy




More information about the Python-list mailing list