Different results with intermediate prints
I have this function
def getDigits (num) :
check = checkNum(num)
#print('num: '+str(num))
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
#print(list(numList))
squareList = map(getSquareOfDigits, numList)
#print(list(squareList))
sumOfSquares = sum(squareList)
print('sumSqr: '+str(sumOfSquares))
getDigits(sumOfSquares)
else :
return check
And the result of it is:
sumSqr: 4
sumSqr: 16
sumSqr: 37
sumSqr: 58
sumSqr: 89
sumSqr: 9
sumSqr: 81
sumSqr: 65
sumSqr: 61
sumSqr: 37
sumSqr: 58
sumSqr: 89
But if I uncomment all the prints, the result is:
[2]
sumSqr: 0
[3]
sumSqr: 0
ok, I know that the list
method change the result, but I want to know it there's a way to print the object variables without changing the result, and not using an intermediate variable.
UPDATE
full example here
python python-3.x
add a comment |
I have this function
def getDigits (num) :
check = checkNum(num)
#print('num: '+str(num))
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
#print(list(numList))
squareList = map(getSquareOfDigits, numList)
#print(list(squareList))
sumOfSquares = sum(squareList)
print('sumSqr: '+str(sumOfSquares))
getDigits(sumOfSquares)
else :
return check
And the result of it is:
sumSqr: 4
sumSqr: 16
sumSqr: 37
sumSqr: 58
sumSqr: 89
sumSqr: 9
sumSqr: 81
sumSqr: 65
sumSqr: 61
sumSqr: 37
sumSqr: 58
sumSqr: 89
But if I uncomment all the prints, the result is:
[2]
sumSqr: 0
[3]
sumSqr: 0
ok, I know that the list
method change the result, but I want to know it there's a way to print the object variables without changing the result, and not using an intermediate variable.
UPDATE
full example here
python python-3.x
2
Can you provide a complete and verifiable example? I suspect something unintended is going on with your return.
– GlobalTraveler
Nov 17 '18 at 11:42
1
Traveler is talking about this. How would we know, whatcheckNum, toInt, getSquareOfDigits
do?
– Mr. T
Nov 17 '18 at 13:07
add a comment |
I have this function
def getDigits (num) :
check = checkNum(num)
#print('num: '+str(num))
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
#print(list(numList))
squareList = map(getSquareOfDigits, numList)
#print(list(squareList))
sumOfSquares = sum(squareList)
print('sumSqr: '+str(sumOfSquares))
getDigits(sumOfSquares)
else :
return check
And the result of it is:
sumSqr: 4
sumSqr: 16
sumSqr: 37
sumSqr: 58
sumSqr: 89
sumSqr: 9
sumSqr: 81
sumSqr: 65
sumSqr: 61
sumSqr: 37
sumSqr: 58
sumSqr: 89
But if I uncomment all the prints, the result is:
[2]
sumSqr: 0
[3]
sumSqr: 0
ok, I know that the list
method change the result, but I want to know it there's a way to print the object variables without changing the result, and not using an intermediate variable.
UPDATE
full example here
python python-3.x
I have this function
def getDigits (num) :
check = checkNum(num)
#print('num: '+str(num))
if check is False :
strNum = str(num)
numList = map(toInt, strNum)
#print(list(numList))
squareList = map(getSquareOfDigits, numList)
#print(list(squareList))
sumOfSquares = sum(squareList)
print('sumSqr: '+str(sumOfSquares))
getDigits(sumOfSquares)
else :
return check
And the result of it is:
sumSqr: 4
sumSqr: 16
sumSqr: 37
sumSqr: 58
sumSqr: 89
sumSqr: 9
sumSqr: 81
sumSqr: 65
sumSqr: 61
sumSqr: 37
sumSqr: 58
sumSqr: 89
But if I uncomment all the prints, the result is:
[2]
sumSqr: 0
[3]
sumSqr: 0
ok, I know that the list
method change the result, but I want to know it there's a way to print the object variables without changing the result, and not using an intermediate variable.
UPDATE
full example here
python python-3.x
python python-3.x
edited Nov 17 '18 at 19:10
asked Nov 15 '18 at 18:05
user10608741
2
Can you provide a complete and verifiable example? I suspect something unintended is going on with your return.
– GlobalTraveler
Nov 17 '18 at 11:42
1
Traveler is talking about this. How would we know, whatcheckNum, toInt, getSquareOfDigits
do?
– Mr. T
Nov 17 '18 at 13:07
add a comment |
2
Can you provide a complete and verifiable example? I suspect something unintended is going on with your return.
– GlobalTraveler
Nov 17 '18 at 11:42
1
Traveler is talking about this. How would we know, whatcheckNum, toInt, getSquareOfDigits
do?
– Mr. T
Nov 17 '18 at 13:07
2
2
Can you provide a complete and verifiable example? I suspect something unintended is going on with your return.
– GlobalTraveler
Nov 17 '18 at 11:42
Can you provide a complete and verifiable example? I suspect something unintended is going on with your return.
– GlobalTraveler
Nov 17 '18 at 11:42
1
1
Traveler is talking about this. How would we know, what
checkNum, toInt, getSquareOfDigits
do?– Mr. T
Nov 17 '18 at 13:07
Traveler is talking about this. How would we know, what
checkNum, toInt, getSquareOfDigits
do?– Mr. T
Nov 17 '18 at 13:07
add a comment |
1 Answer
1
active
oldest
votes
The point is that numList = map(toInt, strNum)
makes numList
a iterator. After it has been iterated over once, it's exhausted and will no longer contain any values. The line print(list(numList))
does exactly that by iterating over it, creating a list, printing a string representation of that list and then throwing everything away. If the print-line is commented, the iterator returned by map
will generate values; with the print
in-place, numList
is effectively empty.
To prevent this, use numList = list(map(toInt, strNum))
; that way, you have a persistent list-object which you can re-use.
ok, good answer. But, what if I want only to see an intermediate result and not add it to my code?
– user10608741
Nov 21 '18 at 17:04
1
I honestly dont understand: Not add it to the code?
– user2722968
Nov 21 '18 at 19:21
in php if I have doubs about what have after some code, I can do a print of the result and see it, without change the code execution, in JS I can do a console log, and so on... I want to do the same in python, see what I have in the middle of the execution without change the variable
– user10608741
Nov 22 '18 at 16:01
As I said above, usenumList = list(map(toInt, strNum))
instead of justnumList = map(...)
. This will causenumList
to be a list-object, not an iterator. You can thenprint(numList)
without side-effects.
– user2722968
Nov 22 '18 at 16:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325461%2fdifferent-results-with-intermediate-prints%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The point is that numList = map(toInt, strNum)
makes numList
a iterator. After it has been iterated over once, it's exhausted and will no longer contain any values. The line print(list(numList))
does exactly that by iterating over it, creating a list, printing a string representation of that list and then throwing everything away. If the print-line is commented, the iterator returned by map
will generate values; with the print
in-place, numList
is effectively empty.
To prevent this, use numList = list(map(toInt, strNum))
; that way, you have a persistent list-object which you can re-use.
ok, good answer. But, what if I want only to see an intermediate result and not add it to my code?
– user10608741
Nov 21 '18 at 17:04
1
I honestly dont understand: Not add it to the code?
– user2722968
Nov 21 '18 at 19:21
in php if I have doubs about what have after some code, I can do a print of the result and see it, without change the code execution, in JS I can do a console log, and so on... I want to do the same in python, see what I have in the middle of the execution without change the variable
– user10608741
Nov 22 '18 at 16:01
As I said above, usenumList = list(map(toInt, strNum))
instead of justnumList = map(...)
. This will causenumList
to be a list-object, not an iterator. You can thenprint(numList)
without side-effects.
– user2722968
Nov 22 '18 at 16:25
add a comment |
The point is that numList = map(toInt, strNum)
makes numList
a iterator. After it has been iterated over once, it's exhausted and will no longer contain any values. The line print(list(numList))
does exactly that by iterating over it, creating a list, printing a string representation of that list and then throwing everything away. If the print-line is commented, the iterator returned by map
will generate values; with the print
in-place, numList
is effectively empty.
To prevent this, use numList = list(map(toInt, strNum))
; that way, you have a persistent list-object which you can re-use.
ok, good answer. But, what if I want only to see an intermediate result and not add it to my code?
– user10608741
Nov 21 '18 at 17:04
1
I honestly dont understand: Not add it to the code?
– user2722968
Nov 21 '18 at 19:21
in php if I have doubs about what have after some code, I can do a print of the result and see it, without change the code execution, in JS I can do a console log, and so on... I want to do the same in python, see what I have in the middle of the execution without change the variable
– user10608741
Nov 22 '18 at 16:01
As I said above, usenumList = list(map(toInt, strNum))
instead of justnumList = map(...)
. This will causenumList
to be a list-object, not an iterator. You can thenprint(numList)
without side-effects.
– user2722968
Nov 22 '18 at 16:25
add a comment |
The point is that numList = map(toInt, strNum)
makes numList
a iterator. After it has been iterated over once, it's exhausted and will no longer contain any values. The line print(list(numList))
does exactly that by iterating over it, creating a list, printing a string representation of that list and then throwing everything away. If the print-line is commented, the iterator returned by map
will generate values; with the print
in-place, numList
is effectively empty.
To prevent this, use numList = list(map(toInt, strNum))
; that way, you have a persistent list-object which you can re-use.
The point is that numList = map(toInt, strNum)
makes numList
a iterator. After it has been iterated over once, it's exhausted and will no longer contain any values. The line print(list(numList))
does exactly that by iterating over it, creating a list, printing a string representation of that list and then throwing everything away. If the print-line is commented, the iterator returned by map
will generate values; with the print
in-place, numList
is effectively empty.
To prevent this, use numList = list(map(toInt, strNum))
; that way, you have a persistent list-object which you can re-use.
answered Nov 17 '18 at 15:16
user2722968user2722968
2,75411637
2,75411637
ok, good answer. But, what if I want only to see an intermediate result and not add it to my code?
– user10608741
Nov 21 '18 at 17:04
1
I honestly dont understand: Not add it to the code?
– user2722968
Nov 21 '18 at 19:21
in php if I have doubs about what have after some code, I can do a print of the result and see it, without change the code execution, in JS I can do a console log, and so on... I want to do the same in python, see what I have in the middle of the execution without change the variable
– user10608741
Nov 22 '18 at 16:01
As I said above, usenumList = list(map(toInt, strNum))
instead of justnumList = map(...)
. This will causenumList
to be a list-object, not an iterator. You can thenprint(numList)
without side-effects.
– user2722968
Nov 22 '18 at 16:25
add a comment |
ok, good answer. But, what if I want only to see an intermediate result and not add it to my code?
– user10608741
Nov 21 '18 at 17:04
1
I honestly dont understand: Not add it to the code?
– user2722968
Nov 21 '18 at 19:21
in php if I have doubs about what have after some code, I can do a print of the result and see it, without change the code execution, in JS I can do a console log, and so on... I want to do the same in python, see what I have in the middle of the execution without change the variable
– user10608741
Nov 22 '18 at 16:01
As I said above, usenumList = list(map(toInt, strNum))
instead of justnumList = map(...)
. This will causenumList
to be a list-object, not an iterator. You can thenprint(numList)
without side-effects.
– user2722968
Nov 22 '18 at 16:25
ok, good answer. But, what if I want only to see an intermediate result and not add it to my code?
– user10608741
Nov 21 '18 at 17:04
ok, good answer. But, what if I want only to see an intermediate result and not add it to my code?
– user10608741
Nov 21 '18 at 17:04
1
1
I honestly dont understand: Not add it to the code?
– user2722968
Nov 21 '18 at 19:21
I honestly dont understand: Not add it to the code?
– user2722968
Nov 21 '18 at 19:21
in php if I have doubs about what have after some code, I can do a print of the result and see it, without change the code execution, in JS I can do a console log, and so on... I want to do the same in python, see what I have in the middle of the execution without change the variable
– user10608741
Nov 22 '18 at 16:01
in php if I have doubs about what have after some code, I can do a print of the result and see it, without change the code execution, in JS I can do a console log, and so on... I want to do the same in python, see what I have in the middle of the execution without change the variable
– user10608741
Nov 22 '18 at 16:01
As I said above, use
numList = list(map(toInt, strNum))
instead of just numList = map(...)
. This will cause numList
to be a list-object, not an iterator. You can then print(numList)
without side-effects.– user2722968
Nov 22 '18 at 16:25
As I said above, use
numList = list(map(toInt, strNum))
instead of just numList = map(...)
. This will cause numList
to be a list-object, not an iterator. You can then print(numList)
without side-effects.– user2722968
Nov 22 '18 at 16:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53325461%2fdifferent-results-with-intermediate-prints%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Can you provide a complete and verifiable example? I suspect something unintended is going on with your return.
– GlobalTraveler
Nov 17 '18 at 11:42
1
Traveler is talking about this. How would we know, what
checkNum, toInt, getSquareOfDigits
do?– Mr. T
Nov 17 '18 at 13:07