[Tutor] Learning python as a thing to do

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Feb 28 00:00:41 CET 2005



On Sun, 27 Feb 2005, Greg T wrote:

> I am a Rubyist, but I've decided to learn Python so that when a
> conversation springs up about the merits of the two languages amd how
> they compare, I will be well informed.

Hi Greg,

Welcome aboard!  That sounds great; you can help us understand Ruby better
when we have questions.  *grin*


> So far, some things I dont care for and have me scratching my head:

Ok, we'll try addressing some of your head-scratching questions:



> immutable strings

Immutable strings aren't unique to Python: technically, string literals in
C are also supposed to be immutable (that's why GCC supports the
'-fno-const-strings' flag!) , and they're also immutable in Java.


The other thing is that Python follows a model where values are objects,
and variable names are just references to values.  So when we do something
like:

###
>>> message = "hello world"
>>> anotherMsg = message
###

Both 'message' and 'anotherMsg' both refer to a single string value.  We
can see this using the id() function:

###
>>> id(message)
3607272
>>> id(anotherMsg)
3607272
###

So there isn't any string copying going on here.


We can simulate mutable strings by turning a string into a list:

###
>>> myBuffer = list("hello world")
>>> myBuffer[1] = 'a'
>>> msg = ''.join(myBuffer)
>>> msg
'hallo world'
###



> no case statement

If you're doing a case analysis based on a single value, then a dispatch
table may be able to do what you want.  You may find:

    http://mail.python.org/pipermail/tutor/2004-December/033728.html

helpful.



> lack of internal iterators

I'm not quite sure what you mean by "internal iterators".  There is an
iterator protocol that objects can support:

    http://www.python.org/doc/lib/typeiter.html

where lists, files, and strings conform to the protocol and can be
iterated across with a 'for' loop.




> Question(s):
>
> Are there any good books/documents that actually examine the ruby way vs
> python way? (by someone that knows both languages)

I haven't seen one yet; if you find one, let us know.  I'm actually quite
interested in Ruby, since a friend of mine is writing Ruby code that I'll
probably have to maintain soon.  *grin*



> The other day I saw a post from a gentleman trying to do a basic prompt
> and add type of calculator. He wanted to assign the +, or * operator to
> a variable to use, but I believe he was told you have to use the literal
> +, or *.

The 'infix' operators aren't first-class in Python.  That being said,
there are first-class function equivalents in the 'operator' module:

    http://www.python.org/doc/lib/module-operator.html

and these are first class, so it's possible to writing something like
this:

###
>>> import operator
>>> sumOfNumbersUpToTen = reduce(operator.add, range(1, 11))
>>> factorialTen = reduce(operator.mul, range(1, 11))
>>> sumOfNumbersUpToTen
55
>>> factorialTen
3628800
###



> In ruby, you can rerence the * operator
> operator = :*
> num1 = 4
> num2 = 6
> num1.send(operator,num2)
> which returns 24

Ok, I see, so in Ruby, numbers can receive a 'multiply' message.  This can
happen in Python too, although it's really not quite Pythonic:

###
>>> num1 = 4
>>> num1.__mul__(6)
24
###

Numbers in Python do have a fixed list of methods that they respond to:

    http://www.python.org/doc/ref/numeric-types.html

but most Python numeric code will just use infix operators on numbers and
avoid an explicit message-passing style.


Best of wishes to you!



More information about the Tutor mailing list