0

Lets see same program written in java and python . I cannot understand the motive behind python's behavior .

prog.py :

#!/usr/bin/python
print "Hello, World!\n"
def hello(name):
    print "Hello " + name
    print sample

#hello("harish")

Prog.java

public class Prog {
    public static void hello(String name) {
       System.out.println("Hello " + name);
       System.out.println(sample); 

    }

    public static void main (String... args) {

       System.out.println("Hello, World!\n");
       //hello("Harish");
    }
}

The above python code prog.py executes without any problem but Prog.java raises errors .

My arguments

1) I would prefer python is good , looking from the angle that I need not bother about it until I call the function hello(name) . If i called the function during execution phase , then an error would be flung as "NameError: global name 'sample' is not defined".

2) But it looks danger by looking at the angle that it might fling such errors in execution time . But java tells all these things even during compile time .

3) also if I want to distribute the python code as .pyc file , using py_compile.compile("prog.py") . The pyc file is generated without warning that 'sample' is an undefined variable . This puts me at risk because these errors might be discovered in production ,which is too dangerous .

What am I to interpret from this type of working of python . Why is it designed to be so ? what is the advantage in not being so strict while compilation (py -> pyc ) ? Is it not dangerous ?

Harish Kayarohanam
  • 3,886
  • 4
  • 31
  • 55
  • What is the question?? –  Feb 21 '14 at 18:04
  • `sample` could be defined globally somewhere else in your program. If you're interested in finding those problems before you run your program, use a tool like [pylint](http://www.pylint.org/). – vanza Feb 21 '14 at 18:04
  • What you are trying to ask is unclear. What differences are you trying to focus on between the two languages? – signus Feb 21 '14 at 18:04
  • 1
    @Signus he's trying to ask why Python allows him to compile bytecode that contains an undefined variable, while Java does not. – Adam Smith Feb 21 '14 at 18:05
  • exactly correct @adsmith . Will this not land me into unnecessary trouble . A simple compile time warning could have saved me .. why was this not done ? – Harish Kayarohanam Feb 21 '14 at 18:05
  • I will rephrase the question to what @adsmith suggested .. – Harish Kayarohanam Feb 21 '14 at 18:08
  • 1
    This question is not a duplicate about static typing version dynamic typing as people have marked it. It's about runtime linking versus compile time linking, and the associated errors that get thrown. It really has nothing to do with typing. – Ted Bigham Feb 21 '14 at 18:43
  • @TedBigham That's why I voted to close this as "primarily opinion based", although the SO message just lists the people that voted to close and the most voted reason only. – Bakuriu Feb 21 '14 at 18:49
  • Thanks @Bakuriu. I agree it's a "primarily opinion based" question. I wish it showed all the vote types. – Ted Bigham Feb 21 '14 at 19:04

1 Answers1

0

I believe in your python example the variable 'sample' could be coming from "global" scope. That means it could be provided by another python script and still be accessible inside your script. The compiler doesn't know this so it can still compile your code and assume you'll provide that variable during "linking".

Java doesn't have any type of global scope so it's able to know right away that the variable will never be there, and can give you a compile time error.

C and C++ (and probably many other languages) work that same way, even if they are type safe.

Ted Bigham
  • 4,237
  • 1
  • 26
  • 31
  • Actually `sample` **must** come from the global scope. Python *does* know which variables are local, which are `nonlocal` and which are `global`. If you see the bytecode of the sample code you'll see that `LOAD_GLOBAL` is used instead of `LOAD_FAST`. – Bakuriu Feb 21 '14 at 18:18
  • Right. The term "could" is from the compiler's perspective. When it sees 'sample' it says "well that's not declared locally, but I'll give the programmer the benefit of the doubt and assume it's global " (as opposed to missing and throwing an error). Yes, once it's compiled, it "must" be provided in global scope at runtime or the program will break, just as the OP is seeing. – Ted Bigham Feb 21 '14 at 18:28