가능할까요?
아이디어는 값을 할당하거나 가져올 때 일부 처리를 수행하는 특수 변수를 원한다는 것입니다. 또한 일반 변수처럼 보이기를 원하므로 점 표기법이 문제입니다.
이것이 실제로 명시 적이 지 않다는 것을 알고 있지만 이것이 Oz-esque Dataflow Variables 를 복제하기 위해 필요한 것입니다 .
이러한 스타일의 데이터 흐름 변수와 같은 것이 이미 파이썬 라이브러리에 구현 된 경우 알려주십시오.
예:
class Promise(object):
def __init__(self):
self._value = 'defalt_value'
@property
def value(self):
#some processing/logic here
return self._value
@value.setter
def value(self, value):
#some processing/logic here
self._value = value
my_promise_obj = Promise()
my_promise = my_promise_obj.value
my_promise = 'Please, set my_promise_obj.value to this string'
print ('Object`s Value: ' + my_promise_obj.value)
print ('My Variable`s value: ' + my_promise)
print ('Has I changed the class attr?: ' + str(my_promise == my_promise_obj))
이 구문은 이러한 목적으로 Python에서 작동하지 않습니다.
my_promise = 'Please, set my_promise_obj.value to this string'
그 이유는 해당 문자열을 가리 키도록 전역 / 로컬 이름을 다시 할당하기 때문입니다. 당신은 그 할당을 연결할 수 없습니다; 그리고 my_promise
할당 이전에 가리키는 대상을 참조하지도 않습니다 .
그러나 선택할 수있는 것이 많습니다. 가장 분명한 것은 다음과 같은 구문입니다.
set
메소드가 있는 객체 :
my_promise.set('Please, set my_promise_obj.value to this string')
메서드, 클로저 또는 __call__
메서드가 있는 객체 :
my_promise('Please, set my_promise_obj.value to this string')
데이터 설명자가있는 객체 :
my_promise.value = 'Please, set my_promise_obj.value to this string'
또는 완전히 미친 것 (여러 가능성의 몇 가지 예) :
다음을 가진 객체 __iadd__
:
my_promise += 'Please, set my_promise_obj.value to this string'
다음을 가진 객체 __xor__
:
my_promise ^ 'Please, set my_promise_obj.value to this string'
다음을 가진 객체 __rrshift__
:
'Please, set my_promise_obj.value to this string' >> my_promise
값을 설정하기 위해 재정의 될 수 있습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다