[Tutor] OO scripting in python...

GADGIL PRASAD /INFRA/INFOTECH GADGILP@INFOTECH.ICICI.com
Thu, 24 May 2001 10:39:09 +0530


This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_001_01C0E40F.A9F54600
Content-Type: text/plain;
	charset="iso-8859-1"

hello,

It seems you know odd perl OO style alright. The pseudo code example is 
really nice.

Back during c++, for study excercise, I had gone abt finding nouns from 
the problem statement, classes from them, and things like Object flow
diagrams, 
network diagrams etc for railway reservation system.

I used concise study material from my institute prepared from some of famous

OO design related books in turn.

I however found, that the diagrams by diff classmates used to be diff from
each 
other just on paper, implimenting in c++ was a diff story all together which
we 
weren't supposed to do (gratefully!). Instead we coded a very small progs
and 
things like 'date' class etc...

Any de facto book you would suggest ?


BTW alan, I already downloaded ur tut a few days back. One of the
articles due to which, I finally jumped in python fray.

/prasad

-----Original Message-----
From: Michael P. Reilly
[mailto:arcege@dsl092-074-184.bos1.dsl.speakeasy.net]
Sent: Wednesday, May 23, 2001 6:44 PM
To: GADGILP@INFOTECH.ICICI.com
Cc: tutor@python.org
Subject: Re: [Tutor] OO scripting in python...


GADGIL PRASAD     /INFRA/INFOTECH wrote
> I am coming from moderate C, basic C++ knowledge, and some perl exp.
> 
> For OO scripting, the basic concepts I learned during c++ seems
> rather unconnected when I read py-tutorial chapter 9 (classes). 
> It seems individual languages like perl, python implement various
> basic concepts of OO in such different ways, with such varying grammer,
> especially with perl, I was totally confused with 
> 'perltoot' (perl- tom christian's O. O. tutorial')
> It seemd c++ is rather straight forward than that.
> 
> I am not that confused after chapter 9, but I don't have OO coding exp., 
> and things are not very clear either.
> 
> Do I need to buy a book to be able to do OO programming
> in python ?
> 
> Can someone pl. explain oo coding in a 10,000 feet overview ?

Well.. first is that there are a lot of different philosophies for
object-oriented programming.  But how I tend to think about it is
that you don't want to think about the programming, concentrate on the
object-oriented design.

"Object-oriented" basically means that instead of procedures and data
structures, your program wants to think of objects and behaviors of
the objects.

One classic example program is a bank.  You have a teller and some
customers, a line to wait in, transactions for each.  In "normal"
(procedural) programming, you would have a structure for the teller,
one for the line (queue), and possibly structures for the transactions
since that data is more important than the customer information.

Something like:
  assign values to teller structure
  define queue for transactions
  check_for_new_transactions(queue)
  until queue is empty:
    next = get_next_transaction(, queue)
    process_transaction(teller, next)
    check_for_new_transactions(queue)
Here, the routines are typically geared to the precise data structure
being passed.

With object-oriented programmer, you would have a teller object, which
would have a queue (with a "interact_with_next_customer" method?), and
customer objects.  The behavior between the tellers and customers is more
important: customer must fill out deposit slip, teller must check account
information (right bank, valid account), customer must pass over money,
teller must return a receipt.

How about:
  create new teller object
  create new queue object, associated with teller object
  queue.check_for_new_users()
  until queue is empty:
    user = queue.get_next_user()
    teller.process_transaction(user)
    queue.check_for_new_users()

Now this doesn't seem all that different.  But let's replace the teller
with an ATM.  The transactions might be the same, but the routines called
would be very different in procedural programming (since most are are
typed languages).  However in object-oriented programming, we can make
an ATM object that behaves just like the teller object and the rest of
the program wouldn't have to change much.  Or if the user was now an
online user and the queue was from a webpage, then those could be more
easily changed as well.

What is commonly thrown about is "code reuse".  As you can see, if
designed correctly, things like the queue, user, teller can be reused
more easily than the functions in the first example (but all can be
reused if all designed correctly).

  -Arcege

PS: IMO, Perl makes things very confusing in general, and how they added
"objects" in Perl5 was especially so.  Congratulations on actually making
it through that document.

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |


. 


------_=_NextPart_001_01C0E40F.A9F54600
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV=3D"Content-Type" CONTENT=3D"text/html; =
charset=3Diso-8859-1">
<META NAME=3D"Generator" CONTENT=3D"MS Exchange Server version =
5.5.2653.12">
<TITLE>RE: [Tutor] OO scripting in python...</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=3D2>hello,</FONT>
</P>

<P><FONT SIZE=3D2>It seems you know odd perl OO style alright. The =
pseudo code example is </FONT>
<BR><FONT SIZE=3D2>really nice.</FONT>
</P>

<P><FONT SIZE=3D2>Back during c++, for study excercise, I had gone abt =
finding nouns from </FONT>
<BR><FONT SIZE=3D2>the problem statement, classes from them, and things =
like Object flow diagrams, </FONT>
<BR><FONT SIZE=3D2>network diagrams etc for railway reservation =
system.</FONT>
</P>

<P><FONT SIZE=3D2>I used concise study material from my institute =
prepared from some of famous </FONT>
<BR><FONT SIZE=3D2>OO design related books in turn.</FONT>
</P>

<P><FONT SIZE=3D2>I however found, that the diagrams by diff classmates =
used to be diff from each </FONT>
<BR><FONT SIZE=3D2>other just on paper, implimenting in c++ was a diff =
story all together which we </FONT>
<BR><FONT SIZE=3D2>weren't supposed to do (gratefully!). Instead we =
coded a very small progs and </FONT>
<BR><FONT SIZE=3D2>things like 'date' class etc...</FONT>
</P>

<P><FONT SIZE=3D2>Any de facto book you would suggest ?</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>BTW alan, I already downloaded ur tut a few days =
back. One of the</FONT>
<BR><FONT SIZE=3D2>articles due to which, I finally jumped in python =
fray.</FONT>
</P>

<P><FONT SIZE=3D2>/prasad</FONT>
</P>

<P><FONT SIZE=3D2>-----Original Message-----</FONT>
<BR><FONT SIZE=3D2>From: Michael P. Reilly</FONT>
<BR><FONT SIZE=3D2>[<A =
HREF=3D"mailto:arcege@dsl092-074-184.bos1.dsl.speakeasy.net">mailto:arce=
ge@dsl092-074-184.bos1.dsl.speakeasy.net</A>]</FONT>
<BR><FONT SIZE=3D2>Sent: Wednesday, May 23, 2001 6:44 PM</FONT>
<BR><FONT SIZE=3D2>To: GADGILP@INFOTECH.ICICI.com</FONT>
<BR><FONT SIZE=3D2>Cc: tutor@python.org</FONT>
<BR><FONT SIZE=3D2>Subject: Re: [Tutor] OO scripting in =
python...</FONT>
</P>
<BR>

<P><FONT SIZE=3D2>GADGIL PRASAD&nbsp;&nbsp;&nbsp;&nbsp; /INFRA/INFOTECH =
wrote</FONT>
<BR><FONT SIZE=3D2>&gt; I am coming from moderate C, basic C++ =
knowledge, and some perl exp.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; For OO scripting, the basic concepts I learned =
during c++ seems</FONT>
<BR><FONT SIZE=3D2>&gt; rather unconnected when I read py-tutorial =
chapter 9 (classes). </FONT>
<BR><FONT SIZE=3D2>&gt; It seems individual languages like perl, python =
implement various</FONT>
<BR><FONT SIZE=3D2>&gt; basic concepts of OO in such different ways, =
with such varying grammer,</FONT>
<BR><FONT SIZE=3D2>&gt; especially with perl, I was totally confused =
with </FONT>
<BR><FONT SIZE=3D2>&gt; 'perltoot' (perl- tom christian's O. O. =
tutorial')</FONT>
<BR><FONT SIZE=3D2>&gt; It seemd c++ is rather straight forward than =
that.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; I am not that confused after chapter 9, but I =
don't have OO coding exp., </FONT>
<BR><FONT SIZE=3D2>&gt; and things are not very clear either.</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Do I need to buy a book to be able to do OO =
programming</FONT>
<BR><FONT SIZE=3D2>&gt; in python ?</FONT>
<BR><FONT SIZE=3D2>&gt; </FONT>
<BR><FONT SIZE=3D2>&gt; Can someone pl. explain oo coding in a 10,000 =
feet overview ?</FONT>
</P>

<P><FONT SIZE=3D2>Well.. first is that there are a lot of different =
philosophies for</FONT>
<BR><FONT SIZE=3D2>object-oriented programming.&nbsp; But how I tend to =
think about it is</FONT>
<BR><FONT SIZE=3D2>that you don't want to think about the programming, =
concentrate on the</FONT>
<BR><FONT SIZE=3D2>object-oriented design.</FONT>
</P>

<P><FONT SIZE=3D2>&quot;Object-oriented&quot; basically means that =
instead of procedures and data</FONT>
<BR><FONT SIZE=3D2>structures, your program wants to think of objects =
and behaviors of</FONT>
<BR><FONT SIZE=3D2>the objects.</FONT>
</P>

<P><FONT SIZE=3D2>One classic example program is a bank.&nbsp; You have =
a teller and some</FONT>
<BR><FONT SIZE=3D2>customers, a line to wait in, transactions for =
each.&nbsp; In &quot;normal&quot;</FONT>
<BR><FONT SIZE=3D2>(procedural) programming, you would have a structure =
for the teller,</FONT>
<BR><FONT SIZE=3D2>one for the line (queue), and possibly structures =
for the transactions</FONT>
<BR><FONT SIZE=3D2>since that data is more important than the customer =
information.</FONT>
</P>

<P><FONT SIZE=3D2>Something like:</FONT>
<BR><FONT SIZE=3D2>&nbsp; assign values to teller structure</FONT>
<BR><FONT SIZE=3D2>&nbsp; define queue for transactions</FONT>
<BR><FONT SIZE=3D2>&nbsp; check_for_new_transactions(queue)</FONT>
<BR><FONT SIZE=3D2>&nbsp; until queue is empty:</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; next =3D get_next_transaction(, =
queue)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; process_transaction(teller, =
next)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; =
check_for_new_transactions(queue)</FONT>
<BR><FONT SIZE=3D2>Here, the routines are typically geared to the =
precise data structure</FONT>
<BR><FONT SIZE=3D2>being passed.</FONT>
</P>

<P><FONT SIZE=3D2>With object-oriented programmer, you would have a =
teller object, which</FONT>
<BR><FONT SIZE=3D2>would have a queue (with a =
&quot;interact_with_next_customer&quot; method?), and</FONT>
<BR><FONT SIZE=3D2>customer objects.&nbsp; The behavior between the =
tellers and customers is more</FONT>
<BR><FONT SIZE=3D2>important: customer must fill out deposit slip, =
teller must check account</FONT>
<BR><FONT SIZE=3D2>information (right bank, valid account), customer =
must pass over money,</FONT>
<BR><FONT SIZE=3D2>teller must return a receipt.</FONT>
</P>

<P><FONT SIZE=3D2>How about:</FONT>
<BR><FONT SIZE=3D2>&nbsp; create new teller object</FONT>
<BR><FONT SIZE=3D2>&nbsp; create new queue object, associated with =
teller object</FONT>
<BR><FONT SIZE=3D2>&nbsp; queue.check_for_new_users()</FONT>
<BR><FONT SIZE=3D2>&nbsp; until queue is empty:</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; user =3D =
queue.get_next_user()</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; =
teller.process_transaction(user)</FONT>
<BR><FONT SIZE=3D2>&nbsp;&nbsp;&nbsp; =
queue.check_for_new_users()</FONT>
</P>

<P><FONT SIZE=3D2>Now this doesn't seem all that different.&nbsp; But =
let's replace the teller</FONT>
<BR><FONT SIZE=3D2>with an ATM.&nbsp; The transactions might be the =
same, but the routines called</FONT>
<BR><FONT SIZE=3D2>would be very different in procedural programming =
(since most are are</FONT>
<BR><FONT SIZE=3D2>typed languages).&nbsp; However in object-oriented =
programming, we can make</FONT>
<BR><FONT SIZE=3D2>an ATM object that behaves just like the teller =
object and the rest of</FONT>
<BR><FONT SIZE=3D2>the program wouldn't have to change much.&nbsp; Or =
if the user was now an</FONT>
<BR><FONT SIZE=3D2>online user and the queue was from a webpage, then =
those could be more</FONT>
<BR><FONT SIZE=3D2>easily changed as well.</FONT>
</P>

<P><FONT SIZE=3D2>What is commonly thrown about is &quot;code =
reuse&quot;.&nbsp; As you can see, if</FONT>
<BR><FONT SIZE=3D2>designed correctly, things like the queue, user, =
teller can be reused</FONT>
<BR><FONT SIZE=3D2>more easily than the functions in the first example =
(but all can be</FONT>
<BR><FONT SIZE=3D2>reused if all designed correctly).</FONT>
</P>

<P><FONT SIZE=3D2>&nbsp; -Arcege</FONT>
</P>

<P><FONT SIZE=3D2>PS: IMO, Perl makes things very confusing in general, =
and how they added</FONT>
<BR><FONT SIZE=3D2>&quot;objects&quot; in Perl5 was especially =
so.&nbsp; Congratulations on actually making</FONT>
<BR><FONT SIZE=3D2>it through that document.</FONT>
</P>

<P><FONT SIZE=3D2>-- </FONT>
<BR><FONT =
SIZE=3D2>+----------------------------------+---------------------------=
--------+</FONT>
<BR><FONT SIZE=3D2>| Michael P. =
Reilly&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; | =
arcege@speakeasy.net&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp; |</FONT>
</P>
<BR>

<P><B><FONT SIZE=3D2>.. </FONT></B>
</P>

</BODY>
</HTML>
------_=_NextPart_001_01C0E40F.A9F54600--