클래스를 호출 할 때 사용되는 매개 변수 중 하나를 사용하여 클래스의 클래스 메서드에 대해 몇 가지 유효성 검사를 수행하려고합니다.데코 레이팅 된 클래스의 상속 된 클래스 메서드에서 cls 동작
이렇게하려면 필자는 필요한 메서드에 데코레이터를 적용 할 클래스에 데코레이터를 사용하는데,이 데코레이터는 함수의 매개 변수 중 하나를 사용하여 유효성 검사 함수를 수행합니다.
이 모든 것이 기본 클래스에서 잘 작동합니다 (이 예제에서는 Parent
이라고합니다).
그러나 Parent
을 상속하는 다른 클래스를 만들면 (이 예제에서는 Child
이라고합니다) 상속 된 데코레이션 클래스 메서드는 더 이상 정상적으로 작동하지 않습니다.
Child
클래스의 클래스 메서드 내에있는 cls
매개 변수는 Child
이 아니지만 대신 Parent
입니다.
Validation passed
<class '__main__.Parent'> is shouting 123
Validation passed
<class '__main__.Parent'> is shouting 321
내 질문
은 다음과 같습니다 : 다음과 같은 결과가 발생합니다 다음 예import inspect
def is_number(word):
if word.isdigit():
print('Validation passed')
else:
raise Exception('Validation failed')
class ClassDecorator(object):
def __init__(self, *args):
self.validators = args
def __decorateMethod(self):
def wrapped(method):
def wrapper(cls, word, *args, **kwargs):
for validator in self.validators:
validator(word)
return method(word, *args, **kwargs)
return wrapper
return wrapped
def __call__(self, cls):
for name, method in inspect.getmembers(cls):
if name == 'shout':
decoratedMethod = self.__decorateMethod()(method)
setattr(cls, name, classmethod(decoratedMethod))
return cls
@ClassDecorator(is_number)
class Parent(object):
@classmethod
def shout(cls, word):
print('{} is shouting {}'.format(cls, word))
@classmethod
def say(cls):
print('{} is talking'.format(cls))
class Child(Parent):
pass
Parent.shout('123')
Child.shout('321')
촬영
Child
의 classmethod 같이 Parent
으로 호출되는 이유는 무엇
- cl
- 이 디자인을 사용하여 원하는 동작을 얻을 수 있습니까?
P. : 파이썬 2.7.10과 파이썬 3.5.2 모두에서이 기능을 시도했으며 동일한 동작을했습니다.
작동했습니다! 도와 주셔서 감사합니다 – Oct