Thanks for all the replies so far.
I was reading through a tutorial on Python and was pretty much confused how their namespaces are handled.
Taking from
this article,
Functions
Next step: Abstraction. We want to give a name to a piece of code, and call it with a couple of parameters. In other words — we want to define a function (or “procedure”. That’s easy. Use the keyword def like this:
def square(x):
return x*x
print square(2) # Prints out 4
For those of you who understand it: When you pass a parameter to a function, you bind the parameter to the value, thus creating a new reference. If you change the “contents” of this parameter name (i.e. rebind it) that won’t affect the original. This works just like in Java, for instance. Let’s take a look at an example:
def change(some_list):
some_list[1] = 4
x = [1,2,3]
change(x)
print x # Prints out [1,4,3]
As you can see, it is the original list that is passed in, and if the function changes it, these changes carry over to the place where the function was called. Note, however the behaviour in the following example:
def nochange(x):
x = 0
y = 1
nochange(y)
print y # Prints out 1
In the first example, the value is passed by reference. But in the second, it's passed by value. Wth is going on actually?
Then this,
def orange_juice():
return x*2
… where a variable (in this case x) is not bound to an argument and is not given a value inside the function, Python will use the value it has where and when the function is called. In this case:
x = 3
y = orange_juice()
# y is now 6
x = 1
y = orange_juice()
# y is now 2
Usually, this is the sort of behaviour you want (though the example above is a bit contrived — you seldom access variables like this.) However, sometimes it can be nice to have something like a static namespace, that is, storing some values from the environment in the function when it is created. The way of doing this in Python is by means of default arguments.
x = 4
def apple_juice(x=x):
return x*2
Here, the argument x is given a default value which is the same as the value of the variable x at the time when the function is defined. So, as long as nobody supplies an argument for the function, it will work like this:
x = 3
y = apple_juice()
# y is now 8
x = 1
y = apple_juice()
# y is now 8
There is not even a scope. I mean, the function can simply access variables outside itself. And the "x=x" default parameter doesn't make any sense to me.
Ok besides all the confusion, the application that I will be creating needs to be distributable. Anty has mentioned Py2exe, so I would like to know if scripts processed by it are neat in the sense that the output is a single executable, or does it needs to be distributed along with other files like its interpreter and libraries. If it's the latter, is there anyway to pack it all into a single file without requiring installation on the user end? What about its filesize?
I'm only expecting myself, a few friends and maybe some strangers out there who is on Windows to use my applications, so being cross-platform or not is not an issue, for now at least.
And, regarding VB.NET, would it be correct to say that at least 90% of Windows users has the .NET framework installed on their computer?