2014-09-25 2 views
-1

Fabric 작업에 호스트 env를 동적으로 설정해야합니다.클래스 'list'는 '__mod__'을 정의하지 않으므로 '%'연산자를 사용할 수 없습니다.

나는이 시도 :

env.hosts = ['%s'] % server_ip_var 

을하지만 난이 오류 :

Class 'list' does not define 'mod', so the '%' operator cannot be used on its instances less...

This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

+1

는 브래킷을 제거합니다'env.hosts의 = '% s'을 (를) %의 server_ip_var'을. 또는 괄호를 사용하십시오 :'env.hosts = ('% s') % server_ip_var' – fredtantini

+2

'%'는 문자열 연산자입니다 :'env.hosts = [ '% s'% server_ip_var]'문자열리스트가 필요할 경우). – chepner

+0

@chepner 답변으로 받아 들일 수 있습니까? – Prometheus

답변

1

그것은 당신이 요청하지만, 문자열로 객체를 변환하는 다양한에서 얻을 수있는 것을 조금 불분명 방법. 당신의 변수가 문자열로 변환 할 수있는 경우 하나의 가능성은 str() 기능을 사용하는 것입니다

env.hosts = [str(server_ip_var)] 

또 다른 (더 나은) 방법을 사용하는 것 Format String Syntax :

env.hosts = ["{}".format(server_ip_var)] 
1

당신이 정말로 원하는 경우 이, 당신은 list를 서브 클래 싱 할 수 있습니다

class FormattableList(list): 
    def __mod__(self, *args, **kwargs): 
     return list(x.__mod__(*args, **kwargs) for x in self) 


print(FormattableList(['a: %s', 'b: %s']) % 123)