Mastering Python

paul paul at subsignal.org
Fri Mar 16 15:51:32 CET 2007


Paul McGuire schrieb:
> What does Python have that C++ doesn't?
> - The biggie: dynamic typing (sometimes called "duck typing").
> Dynamic typing is a huge simplifier for development:
>   . no variable declarations
>   . no method type signatures
>   . no interface definitions needed
>   . no templating for collections
>   . no method overloading by differing argument type signatures
> ("Imagine there's no data types - I wonder if you can...").  What? No
> static type-checking at compile time?  Nope, not really.  If your
> method expects an object of type X, use it like an X.  If it's not an
> X, you may be surprised how often this is not a problem.
But sometimes it is ;) Typical example: input and CGI/whatever. If one
element is checked you'll get a string, if you select multiple (i.e.
checkboxes) you'll get a list. Both support iteration

Now if you iterate over the result:

case 1, input -> "value1":
for elem in input:
  #in real life we might validate here...
  print elem

-> 'v' 'a' 'l' 'u' 'e' '1'

case 2, input -> ["value1", "value2"]
for elem in input:
  print elem

-> "value1" "value2"

cheers
 Paul

Disclaimer: I like python and I write tests but i wish unittest had
class/module level setUp()...









More information about the Python-list mailing list