Return multiple value
May 30, 2019 at 3:56pm UTC
lastchance wrote:And you would be hard pressed to tell a student that (C++):
for ( const auto &x : S )
is more readable than (Python):
for x in S:
Just out of curiosity (if I may hijack this thread): ‘x’ in that Python example would be a copy or a reference?
May 30, 2019 at 4:26pm UTC
Enoizat wrote:Just out of curiosity (if I may hijack this thread): ‘x’ in that Python example would be a copy or a reference?
Ah, "copy or reference" is a C++ term ... it doesn't really have a meaning in Python.
What you get depends on whether the objects in S are mutable or immutable:
1 2 3 4 5 6 7 8 9
S1 = ( 1, 2, 3, 4 ) # tuple of immutable objects
S2 = ( [1], [2], [3], [4] ) # tuple of mutable objects
print( S1, S2, sep='\n' , end="\n\n" )
for x1 in S1:
x1 *= 10
for x2 in S2:
x2 += [0]
print( S1, S2, sep='\n' )
(1, 2, 3, 4)
([1], [2], [3], [4])
(1, 2, 3, 4)
([1, 0], [2, 0], [3, 0], [4, 0])
TBH, I've got used to Fortran (pass by reference by default if the argument is a variable) and C++ (pass by value by default), but Python sometimes leaves me confused!
So, I'll keep Python for post-processing and plotting graphs and stick to Fortran/C++ for heavy number-crunching.
Last edited on May 30, 2019 at 4:35pm UTC
May 30, 2019 at 4:48pm UTC
Thank you for your answer, lastchance.
(BboyCico, sorry for exploiting your thread.)
Topic archived. No new replies allowed.