[Tutor] Language truce

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 28 Jun 2001 11:49:03 -0700 (PDT)


On Thu, 28 Jun 2001, Michael Powe wrote:

> >>>>> "Israel" == Israel Evans <israel@lith.com> writes:
> 
>     Israel> What exactly IS the Python Programming Paradigm---(PPP) :)
>     Israel> ???

Tim Peters has written on "The Python Way", which, tongue-in-cheek, tries
to capture the spirit of Python:

###
    Beautiful is better than ugly. 

    Explicit is better than implicit. 

    Simple is better than complex. 

    Complex is better than complicated. 

    Flat is better than nested. 

    Sparse is better than dense. 

    Readability counts. 

    Special cases aren't special enough to break the rules. 

    Although practicality beats purity. 

    Errors should never pass silently. 

    Unless explicitly silenced. 

    In the face of ambiguity, refuse the temptation to guess. 

    There should be one -- and preferably only one -- obvious way to do
it.
###



> A couple things I mentioned, like you can't increment through loops.

I'm a little confused by what you mean by "incrementing through loops".  
Can you give an example of what you mean?



> But, also, it occurs to me that another part of the paradigm appears
> to be that you can't create new data types, aka structs in C.

We can make new data types work in by using classes.  For example, if we
want to make a Balloon class that has a shape and color, we can do the
following:

###
class Balloon:
    def __init__(self, shape, color):
        self.shape, self.color = shape, color
###


Afterwards, we can treat it as a regular structure:

###
myballoon = Balloon('airplane', 'blue')
print myballoon.color, 'is the color of my balloon.'
balloon_bag = [ Balloon('heart', 'red'),
                Balloon('robot', 'silver'),
                Balloon('blimp', 'black') ]
###

To give our data type interesting behavior, we can tell Python about more
operations.  If we wanted to "add" two balloons together (although I have
NO idea what that would mean... I guess it would be like tying them
together), we could add something to the Balloon definition:


###
class Balloon:
    def __init__(self, shape, color):
        self.shape, self.color = shape, color
    def __add__(self, other):
        return Balloon('%s tied with %s' % (self.shape, other.shape),
                       '%s-%s' % (self.color, other.color))
###

Let's see what happens:


###
>>> b1 = Balloon('tree', 'red')
>>> b2 = Balloon('bear', 'gold')
>>> b1 + b2
<__main__.Balloon instance at 80ccd30>
>>> b3 = b1 + b2
>>> print b3.shape
tree tied with bear
>>> print b3.color
red-gold
###

I'm just clowning around here.  *grin*


Hope this helps!