Python Classes

John Gordon gordon at panix.com
Mon Aug 4 18:44:09 EDT 2014


In <mailman.12627.1407141661.18130.python-list at python.org> Shubham Tomar <tomarshubham24 at gmail.com> writes:

> classes. I understand that you define classes to have re-usable methods and
> procedures, but, don't functions serve the same purpose.
> Can someone please explain the idea of classes 

If a function simply accepts some data, does some calculations on that
data and then returns a value, then you don't need classes at all.  An
example of this might be the square-root function: pass it any number
and it calculates and returns the square root with no other data needed.

But classes do come in handy for other sorts of uses.  One classic example
is employees at a company.  Each employee has a name, ID number, salary,
date of hire, home address, etc.

You can create an Employee class to store those data items along with
methods to manipulate those data items in interesting ways.  The data
items are specific to each separate Employee object, and the methods are
shared among the entire class.

> Can someone please explain what *things *like "__init__", "Object"
> and "self" mean ?

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

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

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

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon at panix.com    watch 'House', or a real serial killer to watch 'Dexter'.





More information about the Python-list mailing list