다음과 같이하고 싶습니다. 컨테이너 클래스 컨테이너가 있는데, 다른 클래스 인 OtherClass를 참조하는 속성 attr이 있습니다. 함수로 객체에 대한 Python 액세스 (__call__이 아님)
x = Container(2)
x.attr # when accessing the attribute - return value from default method
2
x.attr.another() # but also an attribute can be treated as an object
4
x.attr.as_str()
'String: 2'
나는이 어떻게 할 수있는
:class OtherClass:
def __init__(self, value):
self._value = value
def default(self):
retirn self._value
def another(self):
return self._value ** 2
def as_str(self):
return 'String: %s' % self._value
class Container:
def __init__(self, attr):
self.attr = OtherClass(attr)
내가하고 싶어?
. 'attr'이 호출 가능한 메소드'another'를 가진 클래스인지 확인하십시오. 속성, 프록시 또는 메타 클래스로 수행 할 수 없습니다. –