^-- Could not agree more.
Scripting Language
- psi29a
- Posts: 5361
- Joined: 29 Sep 2011, 10:13
- Location: Belgium
- Gitlab profile: https://gitlab.com/psi29a/
- Contact:
Re: Scripting Language
Ok, sorry. I just read general posts. Although since when is SO rep (Still in the top 1% at SO) a good measure of knowledge? (Although the other stuff, sure). However I read his post an no where does he mention that it is pass by reference. In fact he goes as far as to call it "by object reference", which is what I would call passing references by value. This is nothing new, I point you to the Java Language Specification, which was equally written by knowledgeable people, and also uses "References" and exibits this behaviour (Mentioned by Martelli)
and Java Language Specification it clearly states that Java is Pass by Value.
The most common non scientific definition of pass by reference and pass by value I have heard is: can you write a simple swap function? If not then it is pass by value. You can't write a swap function in python without looking at the stack.
Code: Select all
def bar(b): b.append(2)
z = []
bar(z)
print z
>>> [2]
The most common non scientific definition of pass by reference and pass by value I have heard is: can you write a simple swap function? If not then it is pass by value. You can't write a swap function in python without looking at the stack.
Re: Scripting Language
Overall this is a very silly debate though
Re: Scripting Language
I agree... it is really silly because the main problem is that Python is neither pass-by-value or pass-by-reference.
I just got a little annoyed reading that you said that Martelli was ``confused''. Sorry.
I had the honor to meet Martelli at the Europython of the last year; since I was a yellow shirt (people in the organization who help) I could talk with him few times and I can tell you he is seriously sharp and he worked directly in the development of the language. About that... do anyone was in the Europython?
So... I think the best way to close it is... quoting Martelli that explains the semantics:
I executed it and I read again what Martelli said, everyone interested should do the same. I think that once we understood the semantics the actual name is fairly unimportant. The python tutorial calls it ``call by object reference''
I just got a little annoyed reading that you said that Martelli was ``confused''. Sorry.
I had the honor to meet Martelli at the Europython of the last year; since I was a yellow shirt (people in the organization who help) I could talk with him few times and I can tell you he is seriously sharp and he worked directly in the development of the language. About that... do anyone was in the Europython?
So... I think the best way to close it is... quoting Martelli that explains the semantics:
And post a simple python2 code example that shows how Python is different from Java or C++.The semantics of argument passing are _exactly_ identical to that of assignment (binding) to a barename; you can fruitfully see argument passing as local (bare) names of the called function being assigned initial values by the caller (that's exactly what happens, in practice).
I executed it and I read again what Martelli said, everyone interested should do the same. I think that once we understood the semantics the actual name is fairly unimportant. The python tutorial calls it ``call by object reference''
Code: Select all
def f(x):
print id(x)
x = ( x + 1 )
print id(x)
def g(x):
print id(x)
x.append(1)
print id(x)
a = 0
b = []
f(a)
print id(a)
print a
print
g(b)
print id(b)
print b
Re: Scripting Language
That is exactly how Java works, and that example clearly shows object references being passed by value.
Whatever you want to call it, feel free, but it isn't pass by reference.
Whatever you want to call it, feel free, but it isn't pass by reference.
Re: Scripting Language
Isn't D language the "next generation" of code, as opposed to C++? I'm just trying to think outside the box here. People won't always be using C++ and probably D will be the norm. Will you guys be coding/scripting in D or C++? I have no idea what the differences are between the two, though.
Re: Scripting Language
Sorry to be this blunt, but you've just wandered into an art galery and argued that post-modern poetry is the future of architecture.Tes96 wrote:Isn't D language the "next generation" of code, as opposed to C++? I'm just trying to think outside the box here. People won't always be using C++ and probably D will be the norm. Will you guys be coding/scripting in D or C++? I have no idea what the differences are between the two, though.
Re: Scripting Language
Haha, well, shows what I know, huh?werdanith wrote:Sorry to be this blunt, but you've just wandered into an art galery and argued that post-modern poetry is the future of architecture.Tes96 wrote:Isn't D language the "next generation" of code, as opposed to C++? I'm just trying to think outside the box here. People won't always be using C++ and probably D will be the norm. Will you guys be coding/scripting in D or C++? I have no idea what the differences are between the two, though.
Re: Scripting Language
It is not how Java works. In Java you always copy the variable value. For objects those values are references.Yacoby wrote:That is exactly how Java works, and that example clearly shows object references being passed by value.
Whatever you want to call it, feel free, but it isn't pass by reference.
Java calls them references, but they are exactly the same as the C pointers so much the exception is NullPointerException. Just you cannot do arithmetics.
I also posted a piece of code to show it: the int in the f function after the call and the int outside is the same, in Java it would not.
Unfortunately you cannot easily replicate the piece of code in Java as there is no obvious way to get the memory address of a variable (the Python id() ).
It is more similar to C++ call passage denoted with the amp (e.g. void f(T & t); I do not care about the names anymore.)
On the other hand it still works differently. Just read the assignment, Python creates a new name x for the result of the expression (x + 1) the new name does not touch the one outside the function.
Just think whatever you like. I wont bother you anymore about the argument.
Re: Scripting Language
ezzetabi:
You probably have a better understanding of this than me. I will come back and take a better look later when I am less ill and have less coursework. However,
You probably have a better understanding of this than me. I will come back and take a better look later when I am less ill and have less coursework. However,
A better example for Java would be that the functions are void f(T* t); This is because Java is pass by value and not pass by reference. This can be seen in this example: http://ideone.com/QHGkgI also posted a piece of code to show it: the int in the f function after the call and the int outside is the same, in Java it would not.
Unfortunately you cannot easily replicate the piece of code in Java as there is no obvious way to get the memory address of a variable (the Python id() ).
It is more similar to C++ call passage denoted with the amp (e.g. void f(T & t); I do not care about the names anymore.)
On the other hand it still works differently. Just read the assignment, Python creates a new name x for the result of the expression (x + 1) the new name does not touch the one outside the function.