2014-06-15 14 views
-1

내 데코 레이팅 된 함수의 매개 변수가 바뀌고 있습니다. authorized(self, resp)에서 respClientView 개체가되고 selfresp 변수가됩니다. 어떻게이 함수를 메서드로 사용하여 메서드로 사용할 수 있습니까?Python - 클래스의 장식 함수 사용

플라스크 클래스 뷰와 flask_oauthlib을 사용합니다.

기능 코드 :

class ClientView(UserView): 

    @bp.route('/vklogin/authorized') 
    @vk.authorized_handler 
    def authorized(self, resp): 
     if resp is None: 
      return 'Access denied: reason=%s error=%s' % (
       request.args['error_reason'], 
       request.args['error_description'] 
      ) 
     session['oauth_token'] = (resp['access_token'], '') 
     me = self.vk.get('method/users.get?uids={}'.format(resp['user_id'])) 
     return '{}'.format(me.data) 

데코레이터 기능 코드 :

class OAuthRemoteApp(object): 
    def authorized_handler(self, f): 
      @wraps(f) 
      def decorated(*args, **kwargs): 
       if 'oauth_verifier' in request.args: 
        try: 
         data = self.handle_oauth1_response() 
        except OAuthException as e: 
         data = e 
       elif 'code' in request.args: 
        try: 
         data = self.handle_oauth2_response() 
        except OAuthException as e: 
         data = e 
       else: 
        data = self.handle_unknown_response() 

       # free request token 
       session.pop('%s_oauthtok' % self.name, None) 
       session.pop('%s_oauthredir' % self.name, None) 
       return f(*((data,) + args), **kwargs) 
      return decorated 

답변

0

이 코드의 간체 (그리고 실행 가능한) 버전 코드 고려해

class OAuthRemoteApp(object): 
    def authorized_handler(self, f): 
     def decorated(*args, **kwargs): 
      print(self, f, args, kwargs) #1 
      # (<__main__.OAuthRemoteApp object at 0xb74d324c>, <function authorized at 0xb774ba04>, (<__main__.ClientView object at 0xb74d32cc>,), {}) 
      data = 'resp' 
      print((data,) + args) #2 
      # ('resp', <__main__.ClientView object at 0xb74d32cc>) 
      return f(*((data,) + args), **kwargs)    
     return decorated 

vk = OAuthRemoteApp() 

class ClientView(object): 
    @vk.authorized_handler 
    def authorized(self, resp): 
     print(self, resp) #3 
     # ('resp', <__main__.ClientView object at 0xb7452eec>) 
cv = ClientView() 
cv.authorized() 
  1. 공지 사항을 그의 첫 번째 항목

    args = (<__main__.ClientView object at 0xb74d32cc>,) 
    
  2. (data,) + args 앞에 추가 ClientView 인스턴스 앞 'resp' : 53,629,는 ClientView 인스턴스이다. 이것이 문제의 근원입니다.
  3. 여기서 selfresp이고 respClientView인스턴스입니다. 인수가 잘못된 순서로 제공되고 있습니다.

    class OAuthRemoteApp(object): 
        def authorized_handler(self, f): 
         def decorated(*args, **kwargs): 
          data = 'resp' 
          args = args[:1] + (data,) + args[1:] 
          return f(*args, **kwargs) 
         return decorated 
    
    vk = OAuthRemoteApp() 
    
    class ClientView(object): 
        @vk.authorized_handler 
        def authorized(self, resp): 
         print(self, resp) 
    
    cv = ClientView() 
    cv.authorized() 
    

    공급되는 ClientViewresp 인수를 보여주는

    (<__main__.ClientView object at 0xb7434f0c>, 'resp') 
    

    를 산출한다 :이 문제를 해결하기

한가지 방법 args에 제 항목 후 data를 삽입하는 것이다 올바른 순서로

관련 문제