Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75712

Re: Python Classes

From John Gordon <gordon@panix.com>
Newsgroups comp.lang.python
Subject Re: Python Classes
Date 2014-08-04 22:44 +0000
Organization PANIX Public Access Internet and UNIX, NYC
Message-ID <lrp2bp$a5d$1@reader1.panix.com> (permalink)
References <mailman.12627.1407141661.18130.python-list@python.org>

Show all headers | View raw


In <mailman.12627.1407141661.18130.python-list@python.org> Shubham Tomar <tomarshubham24@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@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Python Classes Shubham Tomar <tomarshubham24@gmail.com> - 2014-08-04 14:10 +0530
  Re: Python Classes John Gordon <gordon@panix.com> - 2014-08-04 22:44 +0000
    Re: Python Classes Terry Reedy <tjreedy@udel.edu> - 2014-08-04 19:26 -0400
    Re: Python Classes "Neil D. Cerutti" <neilc@norwich.edu> - 2014-08-05 11:37 -0400
    Re: Python Classes Chris Angelico <rosuav@gmail.com> - 2014-08-06 02:08 +1000

csiph-web