Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
try end ブロックで変数スコープを制限する
{
"cells": [
{
"metadata": {
"toc": true
},
"cell_type": "markdown",
"source": "<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n<div class=\"toc\"><ul class=\"toc-item\"><li><span><a href=\"#try-end-ブロックで変数スコープを制限する\" data-toc-modified-id=\"try-end-ブロックで変数スコープを制限する-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>try end ブロックで変数スコープを制限する</a></span></li></ul></div>"
},
{
"metadata": {},
"cell_type": "markdown",
"source": "# try end ブロックで変数スコープを制限する\n\n一次変数 data をグローバルに宣言したくないようなときに、try end ブロックで括ると良い気がする。"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "import Plots\nPlots.gr()\ntry\n data = rand(10)\n Plots.plot(data)\nend",
"execution_count": 1,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 1,
"data": {
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 600 400\">\n<defs>\n <clipPath id=\"clip4300\">\n <rect x=\"0\" y=\"0\" width=\"600\" height=\"400\"/>\n </clipPath>\n</defs>\n<polygon clip-path=\"url(#clip4300)\" points=\"\n0,400 600,400 600,0 0,0 \n \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip4301\">\n <rect x=\"120\" y=\"0\" width=\"421\" height=\"400\"/>\n </clipPath>\n</defs>\n<polygon clip-path=\"url(#clip4300)\" points=\"\n32.4745,375.869 580.315,375.869 580.315,11.811 32.4745,11.811 \n \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip4302\">\n <rect x=\"32\" y=\"11\" width=\"549\" height=\"365\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 93.3457,375.869 93.3457,11.811 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 215.088,375.869 215.088,11.811 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 336.83,375.869 336.83,11.811 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 458.573,375.869 458.573,11.811 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 580.315,375.869 580.315,11.811 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 32.4745,327.391 580.315,327.391 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 32.4745,246.478 580.315,246.478 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 32.4745,165.564 580.315,165.564 \n \"/>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#000000; stroke-width:0.5; stroke-opacity:0.1; fill:none\" points=\"\n 32.4745,84.6508 580.315,84.6508 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 32.4745,375.869 580.315,375.869 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 32.4745,375.869 32.4745,11.811 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 93.3457,375.869 93.3457,370.408 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 215.088,375.869 215.088,370.408 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 336.83,375.869 336.83,370.408 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 458.573,375.869 458.573,370.408 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 580.315,375.869 580.315,370.408 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 32.4745,327.391 40.6921,327.391 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 32.4745,246.478 40.6921,246.478 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 32.4745,165.564 40.6921,165.564 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 32.4745,84.6508 40.6921,84.6508 \n \"/>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 93.3457, 389.669)\" x=\"93.3457\" y=\"389.669\">2</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 215.088, 389.669)\" x=\"215.088\" y=\"389.669\">4</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 336.83, 389.669)\" x=\"336.83\" y=\"389.669\">6</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 458.573, 389.669)\" x=\"458.573\" y=\"389.669\">8</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:middle;\" transform=\"rotate(0, 580.315, 389.669)\" x=\"580.315\" y=\"389.669\">10</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 331.891)\" x=\"26.4745\" y=\"331.891\">0.2</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 250.978)\" x=\"26.4745\" y=\"250.978\">0.4</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 170.064)\" x=\"26.4745\" y=\"170.064\">0.6</text>\n</g>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:end;\" transform=\"rotate(0, 26.4745, 89.1508)\" x=\"26.4745\" y=\"89.1508\">0.8</text>\n</g>\n<polyline clip-path=\"url(#clip4302)\" style=\"stroke:#009af9; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 32.4745,195.909 93.3457,375.869 154.217,206.108 215.088,163.047 275.959,140.386 336.83,141.662 397.701,282.737 458.573,24.9496 519.444,11.811 580.315,39.6168 \n \n \"/>\n<polygon clip-path=\"url(#clip4300)\" points=\"\n489.608,62.931 562.315,62.931 562.315,32.691 489.608,32.691 \n \" fill=\"#ffffff\" fill-opacity=\"1\"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#000000; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 489.608,62.931 562.315,62.931 562.315,32.691 489.608,32.691 489.608,62.931 \n \"/>\n<polyline clip-path=\"url(#clip4300)\" style=\"stroke:#009af9; stroke-width:1; stroke-opacity:1; fill:none\" points=\"\n 495.608,47.811 531.608,47.811 \n \"/>\n<g clip-path=\"url(#clip4300)\">\n<text style=\"fill:#000000; fill-opacity:1; font-family:Arial,Helvetica Neue,Helvetica,sans-serif; font-size:12; text-anchor:start;\" transform=\"rotate(0, 537.608, 52.311)\" x=\"537.608\" y=\"52.311\">y1</text>\n</g>\n</svg>\n"
},
"metadata": {}
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "data # ここでは data は未定義",
"execution_count": 2,
"outputs": [
{
"output_type": "error",
"ename": "LoadError",
"evalue": "\u001b[91mUndefVarError: data not defined\u001b[39m",
"traceback": [
"\u001b[91mUndefVarError: data not defined\u001b[39m",
"",
"Stacktrace:",
" [1] \u001b[1minclude_string\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::String, ::String\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m./loading.jl:522\u001b[22m\u001b[22m"
]
}
]
}
],
"metadata": {
"kernelspec": {
"name": "julia-0.6",
"display_name": "Julia 0.6.2",
"language": "julia"
},
"toc": {
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"base_numbering": 1,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": true,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"language_info": {
"mimetype": "application/julia",
"file_extension": ".jl",
"version": "0.6.2",
"name": "julia"
},
"gist": {
"id": "",
"data": {
"description": "try end ブロックで変数スコープを制限する",
"public": false
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.