Noob question: Is all this typecasting normal?

James Mills prologic at shortcircuit.net.au
Sun Jan 4 23:17:06 EST 2009


On Mon, Jan 5, 2009 at 1:47 PM, sprad <jsprad at gmail.com> wrote:
> On Jan 3, 6:41 pm, Steven D'Aprano <st... at REMOVE-THIS-
> cybersource.com.au> wrote:
>> The OP comes from a Perl background, which AFAIK allows you to concat
>> numbers to strings and add strings to numbers. That's probably the (mis)
>> feature he was hoping Python had.

I guess perl must have coercing for it's built-in
types ? :) *shrugs* To be honest, doing such operations
doesn't make much sense to me ... It's difficult to
correctly understand what the following expression
should evaluate to:

>>> a = 2
>>> b = 3
>>> c = "foo"
>>> a + b + c
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
>>> "%d %d %s" % (a, b, c)
'2 3 foo'
>>>

> That's correct -- and that's been one of the more difficult parts of
> my transition. Learned C++ in college, spent a few years doing Perl,
> and now all of a sudden type matters again. It's a very different
> philosophy, but I'm determined to stick with it until I have an Aha!
> moment and find something I can do more easily than I can with Perl.

As mentioned string formatting is your friend :)

cheers
James



More information about the Python-list mailing list