Skip to content

Commit 078b1ec

Browse files
committed
Update python高级知识.ipynb
1 parent f35093c commit 078b1ec

File tree

1 file changed

+94
-3
lines changed

1 file changed

+94
-3
lines changed

面试相关/python高级知识.ipynb

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,12 +308,103 @@
308308
"c.greeting()"
309309
]
310310
},
311+
{
312+
"cell_type": "markdown",
313+
"metadata": {},
314+
"source": [
315+
"# 变量范围"
316+
]
317+
},
318+
{
319+
"cell_type": "markdown",
320+
"metadata": {},
321+
"source": [
322+
"Python寻找变量的过程:本地范围->模块范围->Built-in范围"
323+
]
324+
},
311325
{
312326
"cell_type": "code",
313-
"execution_count": null,
327+
"execution_count": 2,
314328
"metadata": {},
315329
"outputs": [],
316-
"source": []
330+
"source": [
331+
"count = 10\n",
332+
"def greeting(flag):\n",
333+
" if flag:\n",
334+
" count = 20\n",
335+
" print(count)"
336+
]
337+
},
338+
{
339+
"cell_type": "code",
340+
"execution_count": 3,
341+
"metadata": {},
342+
"outputs": [
343+
{
344+
"name": "stdout",
345+
"output_type": "stream",
346+
"text": [
347+
"20\n"
348+
]
349+
}
350+
],
351+
"source": [
352+
"greeting(True)"
353+
]
354+
},
355+
{
356+
"cell_type": "code",
357+
"execution_count": 4,
358+
"metadata": {},
359+
"outputs": [
360+
{
361+
"ename": "UnboundLocalError",
362+
"evalue": "local variable 'count' referenced before assignment",
363+
"output_type": "error",
364+
"traceback": [
365+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
366+
"\u001b[0;31mUnboundLocalError\u001b[0m Traceback (most recent call last)",
367+
"Cell \u001b[0;32mIn [4], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mgreeting\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m)\u001b[49m\n",
368+
"Cell \u001b[0;32mIn [2], line 5\u001b[0m, in \u001b[0;36mgreeting\u001b[0;34m(flag)\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m flag:\n\u001b[1;32m 4\u001b[0m count \u001b[38;5;241m=\u001b[39m \u001b[38;5;241m20\u001b[39m\n\u001b[0;32m----> 5\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[43mcount\u001b[49m)\n",
369+
"\u001b[0;31mUnboundLocalError\u001b[0m: local variable 'count' referenced before assignment"
370+
]
371+
}
372+
],
373+
"source": [
374+
"greeting(False) # 会报错,因为在编译时python会创建本地范围的count定义,而运行时却发现没有值"
375+
]
376+
},
377+
{
378+
"cell_type": "markdown",
379+
"metadata": {},
380+
"source": [
381+
"函数内global定义的变量可以修改外部的变量值"
382+
]
383+
},
384+
{
385+
"cell_type": "code",
386+
"execution_count": 6,
387+
"metadata": {},
388+
"outputs": [
389+
{
390+
"name": "stdout",
391+
"output_type": "stream",
392+
"text": [
393+
"20\n",
394+
"20\n"
395+
]
396+
}
397+
],
398+
"source": [
399+
"count = 10\n",
400+
"def greeting(flag):\n",
401+
" global count\n",
402+
" if flag:\n",
403+
" count = 20\n",
404+
" print(count)\n",
405+
"greeting(True)\n",
406+
"print(count)"
407+
]
317408
}
318409
],
319410
"metadata": {
@@ -332,7 +423,7 @@
332423
"name": "python",
333424
"nbconvert_exporter": "python",
334425
"pygments_lexer": "ipython3",
335-
"version": "3.12.1"
426+
"version": "3.9.6"
336427
}
337428
},
338429
"nbformat": 4,

0 commit comments

Comments
 (0)