Following system colour scheme Selected dark colour scheme Selected light colour scheme

Python 개선 제안 한국어 번역

PEP 336 – None을 호출 가능 객체로 만들기

Author:
Andrew McClelland <eternalsquire at comcast.net>
Status:
Rejected
Type:
Standards Track
Created:
28-Oct-2004
Post-History:


Table of Contents

번역·라이선스 안내

이 비공식 한국어 번역은 원문 Copyright 절의 Public Domain 조건에 따라 제공합니다. 원저자와 공식 원문은 그대로 표시합니다. 수정되지 않은 기준 원문 · 공식 최신판

초록

None은 어떤 인자로 호출하더라도 부작용이 없고 None을 반환하는 호출 가능 객체여야 합니다.

BDFL 선언

이 PEP는 거부되었습니다. None을 호출하면 오류가 발생하는 것은 의도된 기능으로 간주됩니다. 이 제안은 자명성, 명확성, 명시성, 필요성의 기준을 충족하지 못합니다. 제시된 Switch 예제는 훌륭하지만 간단한 lambda 정의로도 쉽게 처리할 수 있습니다. 2005년 6월 17일의 python-dev 논의를 참조하십시오 [1].

동기

선택 가능한 동작을 구현할 때, Python 언어가 지향하는 최소주의적 함수형 프로그래밍 목표에 더 부합하는 프로그래밍 스타일을 가능하게 하기 위함입니다.

근거

(1) 호출하기 전에 메서드 테이블 항목을 None과 비교하여 검사하거나, (2) 테이블의 다른 함수와 비슷한 인자를 받는 아무 효과 없는 지역 메서드를 작성하는 대신, 메서드 테이블에서 None을 범용적인 무효과 동작으로 사용할 수 있게 합니다.

의미는 사실상 다음과 같습니다:

class None:

    def __call__(self, *args):
        pass

사용 방법

이전: 함수 테이블 항목을 None과 비교하여 검사하는 방식:

class Select:

    def a(self, input):
        print 'a'

    def b(self, input):
        print 'b'

    def c(self, input):
        print 'c'

    def __call__(self, input):
     function = { 1 : self.a,
           2 : self.b,
           3 : self.c
        }.get(input, None)
     if function:  return function(input)

이전: 아무 효과 없는 지역 메서드를 사용하는 방식:

class Select:

    def a(self, input):
        print 'a'

    def b(self, input):
        print 'b'

    def c(self, input):
        print 'c'

    def nop(self, input):
     pass

    def __call__(self, input):
        return { 1 : self.a,
        2 : self.b,
        3 : self.c
        }.get(input, self.nop)(input)

변경 후:

class Select:

    def a(self, input):
        print 'a'

    def b(self, input):
        print 'b'

    def c(self, input):
        print 'c'

    def __call__(self, input):
     return { 1 : self.a,
        2 : self.b,
        3 : self.c
        }.get(input, None)(input)

참고 문헌