Словарь с классами?

Хорошо, решенный это самостоятельно;

def sout = new StringBuilder(), serr = new StringBuilder()
def proc = 'ls /badDir'.execute()
proc.consumeProcessOutput(sout, serr)
proc.waitForOrKill(1000)
println "out> $sout err> $serr"

дисплеи:

out> err> ls: cannot access /badDir: No such file or directory

8
задан mandroid 30 July 2009 в 18:10
поделиться

2 ответа

Почти. Вы хотите

shapes = {'1':Square, '2':Circle, '3':Triangle} # just the class names in the dict

x = shapes[raw_input()]() # get class from dict, then call it to create a shape instance.
24
ответ дан 5 December 2019 в 06:38
поделиться

Я бы порекомендовал функцию выбора:

def choose(optiondict, prompt='Choose one:'):
    print prompt
    while 1:
        for key, value in sorted(optiondict.items()):
            print '%s) %s' % (key, value)
        result = raw_input() # maybe with .lower()
        if result in optiondict:
            return optiondict[result]
        print 'Not an option'

result = choose({'1': Square, '2': Circle, '3': Triangle})()
1
ответ дан 5 December 2019 в 06:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: