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


Groups > comp.lang.python > #75742

Re: Python Classes

From "Neil D. Cerutti" <neilc@norwich.edu>
Subject Re: Python Classes
Date 2014-08-05 11:37 -0400
References <mailman.12627.1407141661.18130.python-list@python.org> <lrp2bp$a5d$1@reader1.panix.com>
Newsgroups comp.lang.python
Message-ID <mailman.12671.1407253068.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 8/4/2014 6:44 PM, John Gordon wrote:
> 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.

In simple cases like that, functions could do very well by including a 
little bundle of data (probably a dict) as one of the parameters for 
each related function. Classes help here by organizing the functions 
into namespaces, and allowing very convenient and explicit syntax for 
creating objects and using attributes.

In addition, classes provide hooks into almost all of Python's syntax 
and operations, with special methods like __init__, __add__, etc. If you 
want your employees to be comparable using the <, >, == you need to use 
classes.

Classes provide a means for objects to be related, substitutable, and 
interdependent, using inheritance.

Properties work only with classes and provide a convenient way to 
customize attribute retrieval and setting without forcing a change in 
the syntax required for usage.

Classes can be constructed dynamically using metaclasses.

Some of these things can be emulated using just functions and 
mappings--it's what C programmers do--but most of classes in Python can 
do requires language support.

-- 
Neil Cerutti

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