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


Groups > comp.lang.python > #75671 > unrolled thread

Python Classes

Started byShubham Tomar <tomarshubham24@gmail.com>
First post2014-08-04 14:10 +0530
Last post2014-08-06 02:08 +1000
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#75671 — Python Classes

FromShubham Tomar <tomarshubham24@gmail.com>
Date2014-08-04 14:10 +0530
SubjectPython Classes
Message-ID<mailman.12627.1407141661.18130.python-list@python.org>

[Multipart message — attachments visible in raw view] — view raw

Hi,

Python is the first programming language that I'm learning.
I'm confused by the idea of classes and intimidated by syntax defining
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 and what *things *like
"__init__", "Object" and "self" mean ?

Thanks

[toc] | [next] | [standalone]


#75712

FromJohn Gordon <gordon@panix.com>
Date2014-08-04 22:44 +0000
Message-ID<lrp2bp$a5d$1@reader1.panix.com>
In reply to#75671
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'.

[toc] | [prev] | [next] | [standalone]


#75714

FromTerry Reedy <tjreedy@udel.edu>
Date2014-08-04 19:26 -0400
Message-ID<mailman.12656.1407194809.18130.python-list@python.org>
In reply to#75712
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

[toc] | [prev] | [next] | [standalone]


#75742

From"Neil D. Cerutti" <neilc@norwich.edu>
Date2014-08-05 11:37 -0400
Message-ID<mailman.12671.1407253068.18130.python-list@python.org>
In reply to#75712
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

[toc] | [prev] | [next] | [standalone]


#75743

FromChris Angelico <rosuav@gmail.com>
Date2014-08-06 02:08 +1000
Message-ID<mailman.12672.1407254896.18130.python-list@python.org>
In reply to#75712
On Wed, Aug 6, 2014 at 1:37 AM, Neil D. Cerutti <neilc@norwich.edu> wrote:
> 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.

And this is exactly how object orientation is done in C. You just have
a structure that holds the object's state, and the (usually) first
parameter to each function is a pointer to that structure. Actually,
I've done that in high level languages too, specifically to decouple
the code from the state (and thus allow me to load new code from the
disk while maintaining state via what's already in memory - doing this
with classes and objects means importing that state explicitly). The
two notations are exactly the same. Compare:

list.append(l, 'spam')
l.append('spam')

One uses namespacing, the other uses object methods. Same thing!

ChrisA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web