glowing713
Frontend-Deep-Dive
glowing713
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (97)
    • Languages (11)
      • JavaScript ๐Ÿ’› (3)
      • Python ๐Ÿ (4)
      • Java โ˜•๏ธ (3)
      • Swift ๐Ÿงก (1)
    • Computer_Science (1)
      • Computer_Network ๐Ÿ•ธ (1)
    • Web_Frontend (4)
      • Vue.js (1)
    • Problem_Solving (76)
    • Server (1)
      • Spring ๐Ÿ€ (1)
    • AI (2)
      • NLP ๐Ÿ—ฃ (1)
      • AI_Math โž— (1)
    • ๊ฐœ๋ฐœํ™˜๊ฒฝ ๊พธ๋ฏธ๊ธฐ โœŒ (1)
    • ์ƒ๊ฐ์ •๋ฆฌ โœ๐Ÿป (1)

๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

  • ๐Ÿง‘๐Ÿปโ€๐Ÿ’ปGithub

๊ณต์ง€์‚ฌํ•ญ

์ธ๊ธฐ ๊ธ€

ํƒœ๊ทธ

  • Baekjoon
  • ์ด๋ถ„ํƒ์ƒ‰
  • ์นด์นด์˜ค ๊ธฐ์ถœ
  • Python
  • 2019 ์นด์นด์˜ค ๊ฐœ๋ฐœ์ž ๊ฒจ์šธ ์ธํ„ด์‹ญ
  • brute-force
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • bfs
  • Algorithm
  • binary search
  • DP
  • ps
  • Stack
  • boostcampaitech
  • c++
  • mst
  • ์™„์ „ํƒ์ƒ‰
  • ๋™์ ๊ณ„ํš๋ฒ•
  • BOJ
  • Java

์ตœ๊ทผ ๋Œ“๊ธ€

์ตœ๊ทผ ๊ธ€

ํ‹ฐ์Šคํ† ๋ฆฌ

hELLO ยท Designed By ์ •์ƒ์šฐ.
glowing713

Frontend-Deep-Dive

[Python] python์˜ ๋ณ€์ˆ˜ ์Šค์ฝ”ํ”„
Languages/Python ๐Ÿ

[Python] python์˜ ๋ณ€์ˆ˜ ์Šค์ฝ”ํ”„

2021. 12. 2. 23:19

Python์˜ with ๊ฐœ๋…์„ ๊ณต๋ถ€ํ•˜๋‹ค๊ฐ€ ๋ฌธ๋œฉ ๊ถ๊ธˆํ•œ ์ ์ด ์ƒ๊ฒผ๋‹ค.

์•„๋ž˜์˜ ์ฝ”๋“œ์—์„œ contents์™€ my_file ๊ฐ’์ด with ๊ตฌ๋ฌธ์„ ๋ฒ—์–ด๋‚˜๋„ ์ฝํž๊นŒ?

 

with open("text.txt", "r") as my_file:
    contents = my_file.read()

print(my_file)  # <_io.TextIOWrapper name='text.txt' mode='r' encoding='UTF-8'>
print(contents)  # <class 'str'> hello

๋งค์šฐ ์ž˜ ์ฝํžŒ๋‹ค... ์™œ์ง€๐Ÿค”

 


https://stackoverflow.com/questions/45100271/scope-of-variable-within-with-statement

 

Scope of variable within "with" statement?

I am reading only firstline from python using : with open(file_path, 'r') as f: my_count = f.readline() print(my_count) I am bit confused over scope of variable my_count. Although prints work...

stackoverflow.com

def hello():
    for i in range(10):
        print("hi")
    try:
        print(i)
    except IndexError as e:
        print(e)


hello()

 

hello ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ i๋ฅผ ๋ณด๋ฉด ๋งˆ์น˜ ์ง€์—ญ๋ณ€์ˆ˜๋ผ์„œ IndexError๊ฐ€ ๋ฐœ์ƒํ•  ๊ฒƒ ๊ฐ™๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ print(i)๋Š” ์ •์ƒ์ ์œผ๋กœ 9๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

A with statement does not create a scope (like if, for and while do not create a scope either).

As a result, Python will analyze the code and see that you made an assignment in the with statement, and thus that will make the variable local (to the real scope).

 

์ฆ‰, ์ € ์‹œ์ ์—์„œ ๋‚˜๋Š” local variable i๋ฅผ ์„ ์–ธํ•œ ๊ฒƒ์ด ๋œ๋‹ค.(hello ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜)

 

StackOverflow์—์„œ ์•„๋ž˜์˜ ๊ธ€์„ ๋ณด๋ฉด ๊ตณ์ด for loop์˜ control variable์„ ์‚ด๋ ค๋‘˜ ํ•„์š”๊ฐ€ ์žˆ๋А๋ƒ์— ๋Œ€ํ•œ ํ† ๋ก ์ด ๊ฝค ์žˆ๋Š” ๋“ฏํ•˜๋‹ค.

 

https://stackoverflow.com/questions/3611760/scoping-in-python-for-loops

 

Scoping in Python 'for' loops

I'm not asking about Python's scoping rules; I understand generally how scoping works in Python for loops. My question is why the design decisions were made in this way. For example (no pun intende...

stackoverflow.com

๊ทธ๋Ÿฌ๋‚˜, ๋Œ€๋‹ค์ˆ˜๋Š” Python์˜ ๊ฐ„๋‹จํ•œ ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด ํฐ ์˜ค๋ฅ˜ ์—†์ด ๋งŒ์กฑํ•˜๊ณ  ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

๊ฒฐ๋ก 

 

Python์˜ ๋ณ€์ˆ˜ ์Šค์ฝ”ํ”„๋Š” ํ•จ์ˆ˜ ๋ฒ”์œ„์ด๋‹ค.

if, for, while, with์€ ์Šค์ฝ”ํ”„๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, C/C++/Java๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ง€์—ญ๋ณ€์ˆ˜์ผ ๊ฒƒ์ด๋ผ๊ณ  ์˜ˆ์ƒํ•˜๊ณ  ์„ ์–ธํ•œ ๋ณ€์ˆ˜๊ฐ€ ์—ฌ์ „ํžˆ ์œ ํšจํ•  ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

Python์œผ๋กœ ์ฝ”๋”ฉํ•  ๋•Œ ์ด์ ์„ ์œ ์˜ํ•ด์•ผ๊ฒ ๋‹ค.

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋ณ€๊ฒฝ๊ธˆ์ง€ (์ƒˆ์ฐฝ์—ด๋ฆผ)

'Languages > Python ๐Ÿ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Python] Handling (Exception, File, Directory, Data)  (0) 2021.01.22
[Python] Python์˜ ์ž๋ฃŒ๊ตฌ์กฐ  (0) 2021.01.21
[Python] Immutable ๊ฐ์ฒด์™€ Mutable ๊ฐ์ฒด  (0) 2021.01.19
    'Languages/Python ๐Ÿ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • [Python] Handling (Exception, File, Directory, Data)
    • [Python] Python์˜ ์ž๋ฃŒ๊ตฌ์กฐ
    • [Python] Immutable ๊ฐ์ฒด์™€ Mutable ๊ฐ์ฒด
    glowing713
    glowing713

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”