Message Boards Message Boards

0
|
3627 Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Localization of values in Block[] not working

I don't quite understand localization of variable values in Block[]. Executing this code in the front end:

w = 1.11;
Block[{w}, w = 2;  Plot[Sin[w x], {x, 0, 2 Pi}]]

leaves the value of w as 2. If I change it to:

w = 1.11;
Block[{w}, w = 2; Print[Plot[Sin[w x], {x, 0, 2 Pi}]]]

then the value of w is 1.11 after executing, as I expect.

Why is w not restored in the first instance? Any help appreciated... problem is, I know how to fix this case, but now I am unsure of when Block[] does and does not restore values of local variables.

--Phil

POSTED BY: Philip Flanner
5 Replies

Thanks Ilian, that resolved both problems.

POSTED BY: Philip Flanner

..and the difference between our two experiences is that I have the predictive interface turned off... ;-)

POSTED BY: David Reiss

The value of w being 2 is due to an evaluation leak from a predictive interface rule being applied behind the scenes. For now, you may avoid this by turning the suggestion bar off in Preferences -> Interface.

POSTED BY: Ilian Gachevski

I rebooted the computer, and reset Mathematica by deleting the User and UserBase directories, but still get the same result:

enter image description here

Still, good to know it's supposed to work the way I thought. I have been having other issues with this release, for example using Clear[] seems to clear a variable even if it set later as part of the same input cell, unless it is used in the ouput of that cell:

enter image description here

POSTED BY: Philip Flanner

I tried your first example and I get that w is 1.11 after executing it as I'd expect. Try your examples again in a fresh version of Mathematica with a fresh kernel.

enter image description here

POSTED BY: David Reiss
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract