๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๐ŸŒฎ ๐Ÿ’ฌ
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป/python

[python] TypeError: 'type' object is not subscriptable

by ๋ฐ”์ฟ„๋ฆฌ 2024. 5. 2.

 

โœ… ๊ฐœ์š”

 

python3.8 ํ™˜๊ฒฝ์—์„œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰์‹œํ‚ค๋‹ˆ ์•„๋ž˜์™€ ๊ฐ™์€ ์—๋Ÿฌ์™€ ํ•จ๊ป˜ ์‹คํ–‰์ด ์•ˆ๋˜์—ˆ๋‹ค

TypeError: 'type' object is not subscriptable

 

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ง€์ ์„ ๋ณด๋‹ˆ

def test_function1() -> list[str]:
	...
    
def test_function2() -> dict:

 

ํƒ€์ž… ํžŒํŠธ๋ฅผ ์ง€์ •ํ•  ๋•Œ ์ƒ๊ธฐ๋Š” ์˜ค๋ฅ˜

์›์ธ์„ ํ™•์ธํ•ด๋ณด๋‹ˆ, typing ํžŒํŠธ๋Š” python3.9๋ถ€ํ„ฐ ์ง€์›ํ•˜๊ณ  ์žˆ๋‹ค

 

โœ… ๋ฐฉ๋ฒ• 1

์‚ฌ์šฉํ•˜๋Š” python ์„ 3.9๋กœ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ๊ณ 

 

โœ… ๋ฐฉ๋ฒ•2

python typing ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ง€์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

python ๋ฒ„์ „ ์—…์€ ํฐ ๋ง˜ ๋จน๊ณ ํ•ด์•ผํ•˜๋‹ˆ ์ด ๋ฐฉ๋ฒ•์„ ๊ฐ•์ถ”ํ•œ๋‹ค.

from typing import List, Dict

def test_function1() -> List[str]:
	...
    
def test_function1() -> Dict:
	...

 

 

โœ… ๊ฒฐ๊ณผ

 

์ž˜ ์ง„ํ–‰๋จ