Discussion:
[Tutor] who makes FOR loop quicker
John Doe
2015-08-05 07:53:14 UTC
Permalink
To pass by reference or by copy of - that is the question from hamlet.
("hamlet" - a community of people smaller than a village python3.4-linux64)

xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
for x in xlist:
print(xlist)
print("\txlist[%d] = %d" % (i, x))
if x%2 == 0 :
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1

So, catch the output and help, PLEASE, me improve the answer:
Does it appropriate ALWAYS reevaluate the terms of the list on each
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen from
an output) and reduce unreasonable reevaluation, what I must to do for that?
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Joel Goldstick
2015-08-05 12:44:07 UTC
Permalink
Post by John Doe
To pass by reference or by copy of - that is the question from hamlet.
("hamlet" - a community of people smaller than a village python3.4-linux64)
xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
print(xlist)
print("\txlist[%d] = %d" % (i, x))
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1
Does it appropriate ALWAYS reevaluate the terms of the list on each
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen from an
output) and reduce unreasonable reevaluation, what I must to do for that?
You aren't passing anything. the for statement is in the same
namespace as the rest of the code. Passing in python is different
than in C or other languages.

A couple of comments:

setting i = 0, then incrementing at the end of the loop would more
pythonically be done with the enumerate function.
Its generally a bad idea to remove items from and iterable while
interating over it. I'm guessing that this is what is confusing you.
One way to remove items from a list is to create a new list, and
append items you want to it, skipping the ones you don't. You don't
really need the index at all since python interation protocol will
walk through the list for you without worrying about index values
--
Joel Goldstick
http://joelgoldstick.com
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
John Doe
2015-08-06 08:34:51 UTC
Permalink
Can You, please, elaborate this "..Passing in Python is different than
in C or other languages..."

'Cause as far as I know - default major Python's implementation CPython
is written in C.
Post by Joel Goldstick
Post by John Doe
To pass by reference or by copy of - that is the question from hamlet.
("hamlet" - a community of people smaller than a village
python3.4-linux64)
xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
print(xlist)
print("\txlist[%d] = %d" % (i, x))
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1
Does it appropriate ALWAYS reevaluate the terms of the list on each
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen from an
output) and reduce unreasonable reevaluation, what I must to do for that?
You aren't passing anything. the for statement is in the same
namespace as the rest of the code. Passing in python is different
than in C or other languages.
setting i = 0, then incrementing at the end of the loop would more
pythonically be done with the enumerate function.
Its generally a bad idea to remove items from and iterable while
interating over it. I'm guessing that this is what is confusing you.
One way to remove items from a list is to create a new list, and
append items you want to it, skipping the ones you don't. You don't
really need the index at all since python interation protocol will
walk through the list for you without worrying about index values
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription o
Joel Goldstick
2015-08-06 12:57:34 UTC
Permalink
Can You, please, elaborate this "..Passing in Python is different than in C
or other languages..."
I hesitate, because this question is usually the fuel of flaming wars.
So in short:

C can pass a value or a reference to a value (the address of the place
in memory where the value is stored)
Python passes an object -- everything in python is an object. If the
object is mutable, and the function mutates it, those results will be
seen outside the function. If the object is immutable, and the
function tries to change its value, a new object is created with the
new value. Its name is the name given in the parameter list -- not
the name that the function was called with. When the function
completes, that object is lost since the outer scoped named object
wasn't changed.
'Cause as far as I know - default major Python's implementation CPython is
written in C.
What language is used for its implementation has nothing to do with
its own specification.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Steven D'Aprano
2015-08-06 14:21:01 UTC
Permalink
Post by Joel Goldstick
Can You, please, elaborate this "..Passing in Python is different than in C
or other languages..."
I hesitate, because this question is usually the fuel of flaming wars.
Very wise :-)

But since I'm not so wise, here are some more comments.
Post by Joel Goldstick
C can pass a value or a reference to a value (the address of the place
in memory where the value is stored)
You are correct that C can pass a reference to a value, namely a
pointer. But from the perspective of the C compiler, that pointer *is*
the value, not the thing being pointed at. So passing a pointer as
argument is no different from passing an int or a float or a bool, it's
just a value, and the C compiler will use pass by value on the pointer
itself.

In C, one can use pointers to *simulate* pass by reference. But this is
not the same thing as actual pass by reference. In pass by reference,
you don't pass (a pointer to the variable you want), you pass (the
variable you want), and the compiler does all the magic needed to make
it work.

Pascal is a good example of pass by reference because it also has
pointers, so we can demonstrate both the real thing and the simulation.
Here is a small Pascal program that uses pass by value, a pointer
simulating pass by reference, and actual pass by reference:


=== cut ===

program demo (input, output);

type
intptr = ^integer;
var
x, y, z: integer;
w: intptr;

function sum(a: integer; b: intptr; var c: integer): integer;
var
total: integer;
begin
total := 0;
total := total + a;
total := total + b^;
total := total + c;
a := -1;
b^ := -1;
c := -1;
sum := total; {set the return value}
end;

begin
x := 100;
y := 100;
z := 100;
w := @y; { address-of operator @ is non-standard Pascal }
writeln(x, ' ', y, ' ', z);
writeln(sum(x, w, z));
writeln(x, ' ', y, ' ', z);
end.


=== cut ===

The output is:

100 100 100
300
100 -1 -1


Note that except for the declaration, inside the body of the function
you treat c as an ordinary variable just like a, while with b you have
to manually dereference the pointer whenever you want to access the int
value. Since C lacks real pass by reference (var c), you have to use the
pointer work-around (b) technique.

Also, assigning to a inside the function has no visible effect since it
is a purely local variable; assignment to b also would have no effect,
since b itself is a local variable, but assignment to what b points to
(b^) is visible outside the function; and of course assignment to c is
visible outside the function.

Pascal "var" parameters are sometimes called output parameters, since
they can be used to pass values back out to the caller.
Post by Joel Goldstick
Python passes an object -- everything in python is an object. If the
object is mutable, and the function mutates it, those results will be
seen outside the function. If the object is immutable, and the
function tries to change its value, a new object is created with the
new value. Its name is the name given in the parameter list -- not
the name that the function was called with. When the function
completes, that object is lost since the outer scoped named object
wasn't changed.
I agree with this paragraph.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
John Doe
2015-08-06 13:28:47 UTC
Permalink
Well, I think, both of us understands that any reference isn't about any
sort of a language. It's about REGISTER = [ALU, FPU, ...]

That's why reference inevitable.

While You're talking about Python - You're talking ONLY about
interpreter for a BYTEcode
Alas, CPU don't speak BYTEcode but BITcode.

So, Python can't allocate memory for CPU only for interpreter, which
will ask allocation through underlying-C-language.

Do I wrong?
CPU have compiler for Python?
As well as multithreading, for instance, in Python goes to a single
interpreter, but in C - to multiple cores of CPU. So Python doesn't have
REAL multithreading, but C - does.

And in my case by means of C-rules Python allocates FOR-loop's list as a
reference. And that mistake wastes each iteration of FOR-loop in
unnecessary RE-evaluation of initial-list IN LOGIC STATEMENT, which must
be created only once. Any INITIATIONS make once. 'Cause it sucks
CPU-memory-allocation-cycle.

Does this point make sense for You?
Post by Joel Goldstick
Can You, please, elaborate this "..Passing in Python is different than in C
or other languages..."
I hesitate, because this question is usually the fuel of flaming wars.
C can pass a value or a reference to a value (the address of the place
in memory where the value is stored)
Python passes an object -- everything in python is an object. If the
object is mutable, and the function mutates it, those results will be
seen outside the function. If the object is immutable, and the
function tries to change its value, a new object is created with the
new value. Its name is the name given in the parameter list -- not
the name that the function was called with. When the function
completes, that object is lost since the outer scoped named object
wasn't changed.
'Cause as far as I know - default major Python's implementation CPython is
written in C.
What language is used for its implementation has nothing to do with
its own specification.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listi
Alan Gauld
2015-08-06 18:54:02 UTC
Permalink
Post by John Doe
Well, I think, both of us understands that any reference isn't about any
sort of a language. It's about REGISTER = [ALU, FPU, ...]
No thats about the implementation.
The language and implemewntation are completely separate.
There can be many different implementations of a single
language and they all have to follow the semantics defined
by the language but are free to build those semantics
any way they like. (And indeed the different versions
of Python do just that!)
Post by John Doe
While You're talking about Python - You're talking ONLY about
interpreter for a BYTEcode
Alas, CPU don't speak BYTEcode but BITcode.
Some do. Some speak higher level things. For example
some CPUs speak Forth. And still others don't use binary
at all but use tri-state values. There werte even some
in the early days that used 4-state values. But these
are all implementation details that the programmer
doesn't need to care about.
Post by John Doe
So, Python can't allocate memory for CPU only for interpreter, which
will ask allocation through underlying-C-language.
Not necessarily. The interpreter could get C to allocate
a huge pool of memory at startup and then use that for
its own allocation/deallocation purposes.
Post by John Doe
CPU have compiler for Python?
Not yet but it is theoretically possible.
And python would not change if someone built one.
Post by John Doe
As well as multithreading, for instance, in Python goes to a single
interpreter, but in C - to multiple cores of CPU.
That depends on the implementation. I have a C compiler
that does not do multi-core/thread working. It's an
implementation detail and the C language does not
specify that it must. It all depends on the code that
the compiler generates.
Post by John Doe
So Python doesn't have
REAL multithreading, but C - does.
Python does. But not all of its implementations do.
The emulate it instead. But so far as the programmer is
concerned his code is using threading/concurrency.
He may need to be aware that the implementation is not
honoring his request fully but that doesn't change his
code.
Post by John Doe
And in my case by means of C-rules Python allocates FOR-loop's list as a
reference.
No, the C implementation might do that. Python as a language
does not. High level languages exist to stop us thinking about
the low level details. The implementation may change, that's not our
problem. Python specifies how the Python execution model works
not how the CPU or the assembler, or the compiler or the
implementer interprets that.

Even C has many different implementations. Some are more efficient
than others. Should we be worrying about which C compiler was used
to build our interpreter? Should we care about whether the CPU
implements multiplication in hardware or in microcode? Or whether it
caches local variables on on-chip cache or uses main memory?

And what about the I/O routines. Do we need to worry about
whether our chosen C compiler is using it's own I/O library,
or calling the BIOS directly? or using the OS system calls?
These are all implementation details that regular
programmers can, and should, ignore.
Post by John Doe
And that mistake wastes each iteration of FOR-loop in
unnecessary RE-evaluation of initial-list IN LOGIC STATEMENT, which must
be created only once. Any INITIATIONS make once. 'Cause it sucks
CPU-memory-allocation-cycle.
In the modern world of fast CPUs and memory and where the vast majority
of applications run in virtual machines(JVM, .Net) and the vast majority
of servers run inside virtualized environments (VMWare etc)
none of that is of the slightest concern to me.

If I was writing code for an embedded system it might be more worrisome,
but then I'd probably not be using Python for that.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
John Doe
2015-08-06 19:15:49 UTC
Permalink
Thank You, Alan.

This is THE FIRST time, when I've got a pleasure from the opponent.
You're maintain status of a thinking human and, as a humble DAOist, I
always say THANK YOU, when I talk to such a Man.
'Cause wisdom bring us the beauty.

So, what else I can add.
Just a little bit.

It would be great to work with You.
You know, life is so shot..
And any talks don't make it better, alas.

Just we can do.
Post by Alan Gauld
Post by John Doe
Well, I think, both of us understands that any reference isn't about any
sort of a language. It's about REGISTER = [ALU, FPU, ...]
No thats about the implementation.
The language and implemewntation are completely searate.
There can be many different implementations of a single
language and they all have to follow the semantics defined
by the language but are free to build those semantics
any way they like. (And indeed the different versions
of Python do just that!)
Post by John Doe
While You're talking about Python - You're talking ONLY about
interpreter for a BYTEcode
Alas, CPU don't speak BYTEcode but BITcode.
Some do. Some speak higher level things. For example
some CPUs speak Forth. And still others don't use binary
at all but use tri-state values. There werte even some
in the early days that used 4-state values. But these
are all implementation details that the programmer
doesn't need to care about.
Post by John Doe
So, Python can't allocate memory for CPU only for interpreter, which
will ask allocation through underlying-C-language.
Not necessarily. The interpreter could get C to allocate
a huge pool of memory at startup and then use that for
its own allocation/deallocation purposes.
Post by John Doe
CPU have compiler for Python?
Not yet but it is theoretically possible.
And python would not change if someone built one.
Post by John Doe
As well as multithreading, for instance, in Python goes to a single
interpreter, but in C - to multiple cores of CPU.
That depends on the implementation. I have a C compiler
that does not do multi-core/thread working. It's an
implementation detail and the C language does not
specify that it must. It all depends on the code that
the compiler generates.
Post by John Doe
So Python doesn't have
REAL multithreading, but C - does.
Python does. But not all of its implementations do.
The emulate it instead. But so far as the programmer is
concerned his code is using threading/concurrency.
He may need to be aware that the implementation is not
honoring his request fully but that doesn't change his
code.
Post by John Doe
And in my case by means of C-rules Python allocates FOR-loop's list as a
reference.
No, the C implementation might do that. Python as a language
does not. High level languages exist to stop us thinking about
the low level details. The implementation may change, that's not our
problem. Python specifies how the Python execution model works
not how the CPU or the assembler, or the compiler or the
implementer interprets that.
Even C has many different implementations. Some are more efficient
than others. Should we be worrying about which C compiler was used
to build our interpreter? Should we care about whether the CPU
implements multiplication in hardware or in microcode? Or whether it
caches local variables on on-chip cache or uses main memory?
And what about the I/O routines. Do we need to worry about
whether our chosen C compiler is using it's own I/O library,
or calling the BIOS directly? or using the OS system calls?
These are all implementation details that regular
programmers can, and should, ignore.
Post by John Doe
And that mistake wastes each iteration of FOR-loop in
unnecessary RE-evaluation of initial-list IN LOGIC STATEMENT, which must
be created only once. Any INITIATIONS make once. 'Cause it sucks
CPU-memory-allocation-cycle.
In the modern world of fast CPUs and memory and where the vast majority
of applications run in virtual machines(JVM, .Net) and the vast majority
of servers run inside virtualized environments (VMWare etc)
none of that is of the slightest concern to me.
If I was writing code for an embedded system it might be more worrisome,
but then I'd probably not be using Python for that.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.pytho

Steven D'Aprano
2015-08-06 13:45:12 UTC
Permalink
Post by John Doe
Can You, please, elaborate this "..Passing in Python is different than
in C or other languages..."
Argument passing in Python is:

- different to Perl, C, Scala, Algol and Pascal;

- the same as Ruby, Lua, Applescript and Javascript;

- the same as Java boxed values (object);

- different to Java unboxed values (machine types).


In C, all values are passed by value. When you pass an argument to a
function, the C compiler makes a copy of that value and passes the
value.

In Pascal, values can be passed by value (like C), or by reference.

The simplest demonstration of pass-by-reference is to write a "swap"
procedure. In Python terms:


# This does not actually work in Python.
def swap(a, b):
tmp = a
a = b
b = tmp

x = 23
y = 42
swap(x, y)
print x, y # prints 42 23

z = 19
swap(x, z)
print x, z # prints 19 42


You *cannot* write a swap procedure like this in Python. The closest you
can do is write a function that returns the two values, then assign
them:

def swap(a, b):
return b, a

x, y = swap(x, y) # this works

but that is not pass by reference.

In Pascal, you can write such a swap procedure.

Scala and Algol use pass by name, and pass by value. This page explains
pass by name in Scala, and how it differs from pass by value:

http://alvinalexander.com/source-code/scala/simple-scala-call-name-example

In Java, unboxed values (not objects, low-level machine ints and floats)
are passed by value, like C.

Python, Ruby, Javascript, Lua, Java boxed values (objects), and many
other languages, all use the same passing style. This has a number of
names:

- pass by object;
- pass by sharing;
- pass by object sharing;

Some people (especially Ruby programmers) call it "pass by reference"
but that is wrong. Others (especially Java programmers) call it "call by
value, where the value is a reference" which is technically correct but
too long. Another name is "call by value/pass by reference", which is
just confusing.

See also:

https://en.wikipedia.org/wiki/Evaluation_strategy

In pass by object sharing, the argument is evaluated but *not* copied.
Since the argument is not copied, it is not pass-by-value. Inside the
function, you can modify the object, and since it is not a copy, the
original sees the changes. But *assignment* to the local variable inside
the function does not affect the caller's variable, so it is not
pass-by-reference.

To summarise:

Pass by value:
- Argument is copied? YES
- Assignment inside function affects original? NO
- Mutation of argument inside function affects original? NO

Pass by reference:
- Argument is copied? NO
- Assignment inside function affects original? YES
- Mutation of argument inside function affects original? YES

Pass by object sharing:
- Argument is copied? NO
- Assignment inside function affects original? NO
- Mutation of argument inside function affects original? YES
Post by John Doe
'Cause as far as I know - default major Python's implementation CPython
is written in C.
That is irrelevent. The argument passing strategy of a language is part
of the language itself, not the implementation language.

C does not allow variables to change type. But Python does. You cannot
do this in C:

x = 23 # x is an integer
x = "foo" # and now it is a string

so clearly the behaviour of a programming language is not always the
same as that of the implementation language.
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
John Doe
2015-08-06 14:02:28 UTC
Permalink
Thank You, Steven.

I've already written to Your colleague, so You will can see about.

And when I'm saying 'ALLOCATION' I keep in mind the REGISTER, not a
glossary or thesaurus. Language is created for us, not for CPU.
Do You agree?

Passing VALUE is a time-expensive procedure. Python can't reach
processor, so any Python's memory allocations don't have sense.

Language always was, is and will be the ONE - compiled bitcode.
Others - just syntax + specially-sphere-applied variations for Your
pleasure.

Isn't it?
Post by Steven D'Aprano
Post by John Doe
Can You, please, elaborate this "..Passing in Python is different than
in C or other languages..."
- different to Perl, C, Scala, Algol and Pascal;
- the same as Ruby, Lua, Applescript and Javascript;
- the same as Java boxed values (object);
- different to Java unboxed values (machine types).
In C, all values are passed by value. When you pass an argument to a
function, the C compiler makes a copy of that value and passes the
value.
In Pascal, values can be passed by value (like C), or by reference.
The simplest demonstration of pass-by-reference is to write a "swap"
# This does not actually work in Python.
tmp = a
a = b
b = tmp
x = 23
y = 42
swap(x, y)
print x, y # prints 42 23
z = 19
swap(x, z)
print x, z # prints 19 42
You *cannot* write a swap procedure like this in Python. The closest you
can do is write a function that returns the two values, then assign
return b, a
x, y = swap(x, y) # this works
but that is not pass by reference.
In Pascal, you can write such a swap procedure.
Scala and Algol use pass by name, and pass by value. This page explains
http://alvinalexander.com/source-code/scala/simple-scala-call-name-example
In Java, unboxed values (not objects, low-level machine ints and floats)
are passed by value, like C.
Python, Ruby, Javascript, Lua, Java boxed values (objects), and many
other languages, all use the same passing style. This has a number of
- pass by object;
- pass by sharing;
- pass by object sharing;
Some people (especially Ruby programmers) call it "pass by reference"
but that is wrong. Others (especially Java programmers) call it "call by
value, where the value is a reference" which is technically correct but
too long. Another name is "call by value/pass by reference", which is
just confusing.
https://en.wikipedia.org/wiki/Evaluation_strategy
In pass by object sharing, the argument is evaluated but *not* copied.
Since the argument is not copied, it is not pass-by-value. Inside the
function, you can modify the object, and since it is not a copy, the
original sees the changes. But *assignment* to the local variable inside
the function does not affect the caller's variable, so it is not
pass-by-reference.
- Argument is copied? YES
- Assignment inside function affects original? NO
- Mutation of argument inside function affects original? NO
- Argument is copied? NO
- Assignment inside function affects original? YES
- Mutation of argument inside function affects original? YES
- Argument is copied? NO
- Assignment inside function affects original? NO
- Mutation of argument inside function affects original? YES
Post by John Doe
'Cause as far as I know - default major Python's implementation CPython
is written in C.
That is irrelevent. The argument passing strategy of a language is part
of the language itself, not the implementation language.
C does not allow variables to change type. But Python does. You cannot
x = 23 # x is an integer
x = "foo" # and now it is a string
so clearly the behaviour of a programming language is not always the
same as that of the implementation language.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription opt
Steven D'Aprano
2015-08-05 13:19:05 UTC
Permalink
Post by John Doe
To pass by reference or by copy of - that is the question from hamlet.
("hamlet" - a community of people smaller than a village python3.4-linux64)
Python *never* uses either pass by reference OR pass by value (copy).
Please read this:

http://import-that.dreamwidth.org/1130.html
Post by John Doe
xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
print(xlist)
print("\txlist[%d] = %d" % (i, x))
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1
It is risky to remove items from a list as you iterate over it. Also,
there is no need to manually increment the index i. Instead, use
enumerate(alist), which returns (index, value) for each value in alist.
But before iterating over the list, make a copy. The easiest way to copy
a list is to take a slice using alist[start:end]. If you leave both
start and end out, it copies the entire list.

First improvement:

for i, x in enumerate(xlist[:]): # list[:] makes a copy of the list
print(xlist)
print("\txlist[%d] = %d" % (i, x))
if x%2 == 0 :
xlist.remove(x)
print(xlist, "\n\n")


But we can improve this even more. Instead of *removing* items we don't
want, we should *add* items we do want. This will almost always be
faster, especially for big lists:

new = []
for i, x in enumerate(xlist): # No need for a copy now.
print("xlist[%d] = %d" % (i, x))
if x%2 != 0 :
new.append(x)
print(new, "\n\n")


And in fact we can write this even more compactly:

new = [x for x in xlist if x%2 != 0]

although in this case you don't get the benefit of printing. This is
called a "list comprehension", and we can break it down:


[x # the expression to be appended to the new list
for x in xlist # the "for loop" part
if x%2 != 0 # optional condition that applies to the expression
]
--
Steve
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Mark Lawrence
2015-08-05 13:27:23 UTC
Permalink
Post by John Doe
To pass by reference or by copy of - that is the question from hamlet.
("hamlet" - a community of people smaller than a village python3.4-linux64)
xlist = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
i = 0
print(xlist)
print("\txlist[%d] = %d" % (i, x))
xlist.remove(x)
print(xlist, "\n\n")
i = i + 1
Does it appropriate ALWAYS reevaluate the terms of the list on each
iteration?
But if I want to pass a copy to FOR instead of a reference (as seen from
an output) and reduce unreasonable reevaluation, what I must to do for that?
From https://docs.python.org/3/tutorial/introduction.html#lists, but
chopped around a little:-

<quote>
All slice operations return a new list containing the requested
elements. This means that the following slice returns a new (shallow)
Post by John Doe
squares = [1, 4, 9, 16, 25]
squares[:]
[1, 4, 9, 16, 25]
</quote>

Hence:-

for x in xlist[:]:
etc.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
John Doe
2015-08-06 14:41:54 UTC
Permalink
Well...
Try this look. But I'm just a human and can make mistakes.:))

Passing value - allocates stack and creates NEW memory position.
Passing reference - makes stack pointer pointing to any position.
Dereference - makes stack pointer pointing to any position AND TAKES VALUE.

So, You can count how much in every case does CPU make steps.
And now add to this BIG ARRAY as input for calculation.

Always must keep in mind, that the NAME of variable exists for SCOPE of
You code, but VALUE - for CPU.

So, reference will be always, unless CPU have reached quantum-mechanics
Post by Steven D'Aprano
Post by Joel Goldstick
Can You, please, elaborate this "..Passing in Python is different than in C
or other languages..."
I hesitate, because this question is usually the fuel of flaming wars.
Very wise :-)
But since I'm not so wise, here are some more comments.
Post by Joel Goldstick
C can pass a value or a reference to a value (the address of the place
in memory where the value is stored)
You are correct that C can pass a reference to a value, namely a
pointer. But from the perspective of the C compiler, that pointer *is*
the value, not the thing being pointed at. So passing a pointer as
argument is no different from passing an int or a float or a bool, it's
just a value, and the C compiler will use pass by value on the pointer
itself.
In C, one can use pointers to *simulate* pass by reference. But this is
not the same thing as actual pass by reference. In pass by reference,
you don't pass (a pointer to the variable you want), you pass (the
variable you want), and the compiler does all the magic needed to make
it work.
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/l
Continue reading on narkive:
Loading...