가져온 다음 호출 할 때 하나의 튜플을 검사하고 수정하는 함수를 만들려고합니다. 나는 이것을 여러 번 부를 수 있기를 바란다. 그러나 변수를 변경하는 방법을 찾을 수 없기 때문에 새 변수를 반환하는 기능 만 있습니다. 그러나 지금 나는이 작업을 수행해야가져온 함수를 통한 파이썬 변경 수정
**modifier.py**
import variable
def function(new_string):
if new_string not in variable.tuple:
variable.tuple = new_string, + variable.tuple
**variable.py**
import modifier
tuple = ('one','two',)
modifier.function('add this')
modifier.function('now this')
#--> tuple should now equal ('now this', 'add this', 'one', 'two',)
:
**modifier.py**
def function(tuple_old, new_string):
if new_string not in tuple_old:
return new_string, + tuple_old
**variable.py**
import modifier
tuple = ('one','two',)
tuple = modifier.function(tuple, 'add this')
tuple = modifier.function(tuple, 'now this')
#--> tuple now equals ('now this', 'add this', 'one', 'two',)
이 많이 지저분하다
는 여기에 내가 그것을 어떻게 처리할지 두 개의 파일, 내 예입니다. 먼저, 튜플을 직접 바꾸는 대신 오래된 튜플 값을 전달하고 반환 된 값을 얻어야합니다. 작동하지만 DRY가 아니며이 클리너를 만드는 방법이 있어야합니다.
사실 이것은 장고 설정 파일에서 내 미들웨어를 업데이트하는 기능이기 때문에 목록을 사용할 수 없습니다. 또한 나는 이 다른 파일에 대한 기능을 가지고을 가지고 있지 않지만 또한 그것이 가능해야한다고 생각합니다.
def function(new_string):
if new_string not in variable.tuple:
variable.tuple = (new_string,) + variable.tuple
첫 번째 블록에서 코드를 시도해 보셨습니까? –
튜플을 수정할 수 없습니다. – wim
주기적 임포트가 있습니다 – wim