[Tutor] Truckers Log....Me Again

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 20 Jan 2002 22:29:16 -0800 (PST)


On Mon, 21 Jan 2002, Erik Price wrote:

> This is my first post to the list.  I've spent the past couple of
> months learning PHP as my first programming language (except the
> obligatory BASIC from years ago, and a smattering of bash/AppleScript
> -ing) and

Hi Erik, nice to have you here!



> > ##
> > while 1:
> >     read the input x
> >     if x == yes:
> >         do this
> >     elif x == no:
> >         do the other thing
> >     else:
> >         print some warning
> > ###
> 
> Admittedly, I am new so I don't know anything.  But it would seem to
> me that this "while" loop would never end, since the condition "1"  
> evaluates to "true" infinitely.

You're right: as it stands, this while loop says nothing about being able
to get out of it.  *grin* Let's talk a little more about the details.



Conventionally, there's an idiom we can use to be able to get out, or
"break" out of this loop.  Here's a concrete example of this in the
interpreter:

###
>>> def readPassword():
...     while 1:
...         print "password?"
...         p = raw_input()
...         if p == 'elbereth':
...             break
...         print "no good, try again"
... 
>>> readPassword()
password?
sega
no good, try again
password?
griffindor
no good, try again
password?
elbereth
###

This is a function that won't let us go until we've said some magic word.  
If we want to get out of a loop, we can use the 'break' statement.  In our
case, 'break' pulls us out of the "infinite" loop.  (Perhaps it's called
"break" because tries to put the "breaks" on a runaway train?  *grin*)



> I would assume that this is for the sake of example, and not to
> demonstrate an actual program's pseudocode.  But on this list I have
> seen more than one example use this convention.  Is Python different
> from other languages in this respect?

Sorta.  Python has two main "looping" or "iterative" syntax structures ---
the 'while' loop and the 'for' loop.  From what I remember, Python's for
loop looks very similar to your bash shell's, so you should feel pretty
comfortable with it once you see it.

###
for name in ['humbert humbert', 'john nash', 'art doyle']:
    print "hello", name
###

is an example of a Python for loop that goes over every element in a
list of names.



One of Python's main design points is that 'statements' --- things like
variable assignment --- can't be plugged into things that expect
'expressions'.  For example, something like:

    while ( (p = raw_input()) != "elbereth" ):

isn't allowed in Python.  If you come from a Perl/C/C++/Java background,
this may be a little shocking to you.


One side effect of this restriction is that certain bug-ridden constructs
like:

###
if x = 42:
    print "that's a magic number"
###

can be flagged by Python as a syntax error, since the programmer probably
meant to check for equality using '==', not assignment.


I don't know if this quite answers your question, but it does touch on one
of the "big" differences that people often run into when they first
encounter Python.





> Another question I have is about objects.  I appreciate the "dumb
> question" thread from earlier this weekend, since I do not completely
> grasp the notion of objects myself.

Ah!  Keep the questions coming, and bring us back to earth.  *grin* These
questions are not dumb, and I personally like chattering about this stuff.



> One of the reasons Python is recommended so heavily is that it is
> "true object oriented".

Python does allow a lot of kinds of programming, including programming
that focuses on objects... but you don't have to jump into object oriented
stuff until you need to.  We can program productively without explicitely
using object oriented programming (OOP).




> And in response to 14-yr old Jimmy's request for programming
> assistance, another python-tutor-lister recommended becoming familiar
> with the use of objects.  Is the use of objects somehow more tightly
> integrated into Python than in other languages such as PHP and Perl?

I can't say anything about PHP.  Python has support for the creation of
classes as a part of the language itself.  On the other hand, Perl uses
some preexisting concepts in its language to make writing OOP classes fit
with the rest of the system.

Using objects is about equally convenient in both Python and Perl.  Here's
a way of getting a file object in Python and printing a line:

    f = open("somefile")
    print f.readline()

and in Perl, it's about the same:

    $f = IO::File->new("somefile");
    print $f->getline();



> I had assumed that I could learn Python's syntax and hopefully get
> started with some small-scale development as a learning exercise, and
> worry about objects later.  But is it a better idea to try to tackle
> this topic as early on as possible?

My opinion: go with your plan.  You can pick up objects as you go along.  
A lot of the advantage of having objects is being able to organize
programs into managable pieces, but you won't run into the managability
problem until much later, and by then, objects should make more sense.  
You might need to know just enough to do things with files:

###
>>> f = open("/home/dyoo/.bash_profile")
>>> f.readline()
'# ~/.bash_profile: executed by bash(1) for login shells.\n'
>>> f.readline()
'# see /usr/share/doc/bash/examples/startup-files for examples\n'
###

but that's about it.  You can write good programs without writing classes.  

Now, functions are a different matter.  Definitely learn functions cold.  
*grin*


Good luck to you.