분류 전체보기980 Python 키워드 매개변수로 사전을 함수에 전달하기, Passing a dictionary to a function as keyword parameters 질문 파라미터에 대응하는 키-값 쌍을 가진 사전을 사용하여 파이썬에서 함수를 호출하고 싶습니다. 다음은 일부 코드입니다: d = dict(param='test') def f(param): print(param) f(d) 이 코드는 {'param': 'test'}를 출력하지만, test만 출력되도록 하고 싶습니다. 더 많은 파라미터에 대해서도 비슷하게 작동하도록 하고 싶습니다: d = dict(p1=1, p2=2) def f2(p1, p2): print(p1, p2) f2(d) 이게 가능할까요? 답변 끝내 스스로 해결했습니다. 간단한 문제였는데 딕셔너리를 언팩하기 위해 ** 연산자를 빠뜨렸던 것이었습니다. 그래서 나의 예제는 다음과 같이 됩니다: d = dict(p1=1, p2=2) def f2(p1,p2.. 2023. 10. 30. Python *args와 **kwargs에 대한 타입 주석, Type annotations for *args and **kwargs 질문 I'm trying out Python's type annotations with abstract base classes to write some interfaces. Is there a way to annotate the possible types of *args and **kwargs? For example, how would one express that the sensible arguments to a function are either an int or two ints? type(args) gives Tuple so my guess was to annotate the type as Union[Tuple[int, int], Tuple[int]], but this doesn't work. fr.. 2023. 10. 30. Python 오프셋이 없는 날짜와 오프셋을 인식하는 날짜를 빼는 것은 불가능합니다., Can't subtract offset-naive and offset-aware datetimes 질문 PostgreSQL에서는 시간대를 인식하는 timestamptz 필드가 있습니다. 테이블에서 데이터를 가져올 때 현재 시간을 빼서 나이를 구하려고 합니다. 문제는 datetime.datetime.now()와 datetime.datetime.utcnow() 모두 시간대를 인식하지 않는 타임스탬프를 반환하는 것 같아서 다음과 같은 오류가 발생합니다: TypeError: can't subtract offset-naive and offset-aware datetimes 이를 피하는 방법은 있을까요 (가능하면 제3자 모듈을 사용하지 않고)? 편집: 제안해주신 방법에 감사드립니다. 하지만 시간대를 조정하려고 시도하면 오류가 발생하는 것 같아서 PG에서 시간대를 인식하지 않는 타임스탬프를 사용하고 항상 다음과 .. 2023. 10. 30. Python django order_by 쿼리셋, 오름차순 및 내림차순, django order_by query set, ascending and descending 질문 django에서 쿼리 세트를 날짜별로 내림차순으로 정렬하는 방법은 무엇인가요? Reserved.objects.all().filter(client=client_id).order_by('check_in') 내가 원하는 것은 check_in 날짜별로 모든 예약을 내림차순으로 필터링하는 것뿐입니다. 답변 Reserved.objects.filter(client=client_id).order_by('-check_in') 알아두세요. check_in 앞에 -가 있습니다. -는 열 이름 앞에 있을 때 "내림차순"을 의미하며, -가 없을 때는 "오름차순"을 의미합니다. Django 문서 2023. 10. 30. 이전 1 ··· 33 34 35 36 37 38 39 ··· 245 다음