L8 Python (Library)

  • Mojo의 진가는 python을 너무나도 쉽게 사용할 수 있다는 것에서 옵니다.
  • 특히 파이썬에서 내가 짠 코드, 혹은 라이브러리(cv2, numpy, torch …)를 가지고 올 수도 있습니다.
from python import Python
from python.object import PythonObject

%%python
def type_printer(my_list, my_tuple, my_int, my_string, my_float):
	print(type(my_list))
	print(type(my_tuple))
	print(type(my_int))
	print(type(my_string))
	print(type(my_float))
  

def main(): 
# 파이썬 모듈은 dynamic함으로 fn이 아니라 def로 선언합니다.
# 파이썬 모듈을 손쉽게 불러올 수 있습니다.

	let np = Python.import_module('numpy')
	array = np.array([1,2,3])
	print(array)

	type_printer(
		[0, 3], 
		(False, True), 
		4, 
		"orange", 
		3.4, 
		array
	)

	# <class 'list'>
	# <class 'tuple'>
	# <class 'int'>
	# <class 'str'>
	# <class 'float'>
	# <class 'numpy.ndarray'>
var dictionary = Python.dict()
dictionary['name'] = 'junseo'
dictionary["job"] = "programmer"
var keys: PythonObject = ["name", "job", "gender"]
var N: Int = keys.__len__().__index__()

print(N, "개의 키가 있습니다.")

for i in range(N): # for문은 파이썬과 동일하게 Mojo에서도 사용할 수 있습니다.
	if Python.is_type(dictionary.get(keys[i]), Python.none()):
		# 파이썬의 `if dictionary.get(keys[i]) == None:`과 동일한 결과
		print(keys[i], "키는 없습니다.")
	else:
		print(keys[i], "키는 있습니다.")

Copyright © 2023 Junseo Ko. 본 내용은 모두 제가 작성한 도큐먼트입니다. 무단 퍼가기와 재생산을 금지합니다. 문의: piglets.frizzle0v@icloud.com