django의 lazy 함수 이해하기 source: django/utils/functional.pylaza함수가 하는 일은 callable객체를 lazy evaluated callable 객체로 바꿔준다. 자세한건 아래의 코드를 보자. 주석 앞에 달린 순서대로 보면 보기 쉽다.#1 함수와 resultclasses를 입력으로 받는다. def lazy(func, *resultclasses): """ Turn any callable into a lazy evaluated callable. result classes or types is required -- at least one is needed so that the automatic forcing of the lazy evaluation code is tr..
Meta class파이썬에서 object는 "class"라 불리는 object에 의해 생성된다. 즉 다시말해 class도 object이다.class도 object이기 때문에 object와 같은 방식으로 필드를 추가하거나 삭제하는 연산이 가능하다.class MyClass: pass obj = MyClass() MyClass.val = 3 MyClass.print = lambda self : print("Hello") obj.print() ---------------------------------- Hello다만 차이점은 class의 필드를 수정하면 이미 생성되 있던 instance도 포함해서 모든 instance들이 같이 수정된다. 이러한 class object를 만들어내는 또 다른 speci..
Iterator (이터레이터) : 한번에 하나의 값을 반환하는 객체iterable : 가지고 있는 값을 한번에 하나씩 반환할 수 있는 객체 (e.g. list, tuple..) 리스트나 튜플은 한번에 모든 값을 반환하므로 iterable 객체이지만 iterator는 아니다. 내장 함수 iter를 사용하면 iterator 객체로 사용할 수 있다.l =[1, 2, 3, 4] pritn(type(iter(l))) ---------------------- list_iteratoriterator에서 값을 한개씩 받는 방법내장 함수 next를 사용한다.l =[1, 2, 3, 4] l_iter = iter(l) print(next(l_iter)) print(next(l_iter)) ---------------..