[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

dynamic type variable

J. Peng

1/21/2008 2:38:00 AM

Python's variable is dynamic type,is it?
But why this can't work?

>>> 3 + 'a'
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand type(s) for +: 'int' and 'str'


So I see the number 3 can't be converted to string type automacially.
2 Answers

Paddy

1/21/2008 4:22:00 AM

0

On Jan 21, 2:37 am, "J. Peng" <jp...@block.duxieweb.com> wrote:
> Python's variable is dynamic type,is it?
> But why this can't work?
>
> >>> 3 + 'a'
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
>
> So I see the number 3 can't be converted to string type automacially.

Hi,
You are probably confusing Dynamic typing with Weak typing.
Python is both dynamically and strongly typed. Perl is
both dynamically and weakly typed. It is Perls weak typing
that allows automatic conversion between dissimilar types.

See: http://en.wikipedia.org/wiki/T...

- Paddy.

Grant Edwards

1/21/2008 4:08:00 PM

0

On 2008-01-21, J. Peng <jpeng@block.duxieweb.com> wrote:

> Python's variable is dynamic type,is it?
> But why this can't work?
>
>>>> 3 + 'a'

It can't work because the compiler has no way of knowing
whether the correct answer is '3a', 0x0d, the color purple, or
to reboot.

> So I see the number 3 can't be converted to string type automacially.

Because I wanted the string 'a' to be converted to an integer
automatically.

--
Grant Edwards grante Yow! A can of ASPARAGUS,
at 73 pigeons, some LIVE ammo,
visi.com and a FROZEN DAQUIRI!!