views:

230

answers:

6

Possible Duplicate:
What’s with the love of dynamic Languages

I'm coming from a c#/java background i.e. strongly typed, OOP language.

I'm very much interested in Python, but I need to learn a little more about the advantages of a dynamic language.

What power does it really give me? (in web applications).

Can someone outline some of the advantages and cool tricks I can do?

+2  A: 

I enjoyed reading this comparison between Python and Java.

In relation with web, I would recommend doing a simple example with Django to see how it works.

Macarse
There are plenty of awesome things about Python, but most of the complaints on that blog post are pretty inane. I do think that the lack of default parameters is a shortcoming, but claiming that he spends "5x as much time" fixing syntax errors due to parentheses and braces in Java means either he's doing it wrong or is not very practiced in writing Java quickly. Also, it doesn't really cover the "dynamic" aspect which is what the OP was asking about in the first place.
Steven Schlansker
+1 for the links, but note that the comparison is really more a list of the flaws of java than anything else. It's more of an argument against java than an argument for python.
John Knoeller
A: 

Can't speak for python per se, but I was playing around with the PSObject class in Powershell last week which allows you to dynamically add members, methods, etc. Coming from a C++\C# background, this seemed like magic - no need to re-comile to get these constructs in-built making it a much nicer workflow for what I was doing.

cristobalito
A: 

Python is strongly typed and object oriented the difference is that Python is also dynamic.

In Python classes are objects like everything else and as every other object you can create and modify them at runtime. This basically means that you can create and change modules, metaclasses, classes, attributes/methods and functions at runtime. You can add base classes to already existing classes and several other things.

DasIch
so why would I want to do that? sounds interesting but don't see the implications clearly?
Blankman
A: 

It's not webapplication related but i think it shows what a dynamic language gives you.

Quoting this famous post:

Task:
Read a file in , and print it out to screen

Python:

#!/usr/bin/python
filename = "readAFile.py"
try:
 for line in open(filename, 'r').readlines(): print line
except: print "Problem with %s" % filename

Java:

import java.io.*;
class readAFile
{
  public static void main(String args[])
  {
    String filename = new String("readAFile.java"); 
    File n = new File("readAFile.java");
      try{
       FileReader fr = new FileReader(n);
    BufferedReader br=new BufferedReader(fr);  
    String line="";
    while( (line=br.readLine()) !=null )
    {System.out.println(line);}   
      }
      catch (FileNotFoundException fnfe) 
   { 
    System.out.println("We're sorry , we can't find your file right now. ");
   }
      catch(IOException ioe)
   {
    System.out.println("I/O Exception with your file mate");
     }
 }
}

Take your conclusion.

--EDIT, not by OP--

To bring some semblance of balance to this post, here's an alternative Java implementation, which is somewhat less excessively verbose :-p

import java.io.*;
import org.apache.commons.io.IOUtils;
public class CatFile {
    public static void main(String[] args) throws IOException {
        IOUtils.copy(new FileReader(new File("/usr/share/dict/words")), System.out);
    }
}
systempuntoout
This example really has nothing to do with the dynamic nature of the languages which the OP is more concerned about.
Cem Catikkas
The Java code is far more verbose than it needs to be. It smells of something written just to show how verbose you *can* make Java. Who's ever used new String("something") in an actual program?
Steven Schlansker
It doesn't even use proper Java indentation style just to fabricate an extra 4 lines of code out of thin air. Ridiculous.
Jörg W Mittag
I was sure i could be exposed in comments like that; please have a look at the thread i've suggested and this by @Jeff on coding horror http://www.codinghorror.com/blog/2005/08/are-all-programming-languages-the-same.html.@Cem this example show you why i loved Python after 6 years of Java coding; like it or not Java static typing generally requires extra typing than Python dynamic typing counterpart.@Jörg i agree that this example is a little bit forced; anyway i don't think it changes the nitty-grytty of the comparison.
systempuntoout
+1  A: 

Python (like all dynamic languages) defers attribute lookups until runtime. This allows you to break past the ideas of polymorphism and interfaces, and leverage the power of duck-typing, whereby you can use a type that merely looks like it should work, instead of having to worry about its ancestry or what it claims to implement.

Ignacio Vazquez-Abrams
+5  A: 

I don't think of dynamically typed languages as "allowing cool tricks" (they do, but mostly it's not really sound to use "cool" tricks in production software -- they come in handy for testing, debugging, etc, but when it comes to getting good, fast stuff deployed for production, simplicity rules).

Rather, I think of such languages as "not getting in my way" -- in particular, not slowing me down by forcing me to redundantly specify things over and over. Not every statically typed languages does "get in your way" -- good ones with solid, logically correct type systems like Haskell let the compiler deduce types (though you may redundantly specify them if you like redundancy... or, more to the point, if you want stricter constraints than what the compiler can actually deduce from the code). But in Java (and to a lesser extent in C# except when you use the reasonably recent var keyword) redundancy is the rule, and that impacts productivity.

A compromise can be offered by third-party checking systems for Python, like typecheck -- I don't use it, myself, but I can see how somebody who really thinks static type checking adds a lot of value might be happy with it. There's even a syntax (which the Python compiler accepts but does nothing with) in recent Python versions to let you annotate your function arguments and return values -- its purpose is to let such packages as typecheck be extended to merge more naturally with the language proper (though I don't think typecheck does yet).

Edit:

As I wrote here, and I quote:

I love the explanations of Van Roy and Haridi, p. 104-106 of their book, though I may or may not agree with their conclusions (which are basically that the intrinsic difference is tiny -- they point to Oz and Alice as interoperable languages without and with static typing, respectively), all the points they make are good. Most importantly, I believe, the way dynamic typing allows real modularity (harder with static typing, since type discipline must be enforced across module boundaries), and "exploratory computing in a computation model that integrates several programming paradigms".

"Dynamic typing is recommended", they conclude, "when programs must be as flexible as possible". I recommend reading the Agile Manifesto to understand why maximal flexibility is crucial in most real-world application programming -- and therefore why, in said real world rather than in the more academic circles Dr. Van Roy and Dr. Hadidi move in, dynamic typing is generally preferable, and not such a tiny issue as they make the difference to be. Still, they at least show more awareness of the issues, in devoting 3 excellent pages of discussion about it, pros and cons, than almost any other book I've seen -- most books have clearly delineated and preformed precedence one way or the other, so the discussion is rarely as balanced as that;).

Alex Martelli