저는 파이썬 코더가 아니지만 사용하는 언어에 관계없이 associative array 데이터 구조로 문제를 해결해야하는 것으로 보입니다. 구조체의 실제 이름은 언어마다 다를 수 있습니다. 예를 들어 C++에서는 map
, 파이썬에서는 .. dictionary! 따라서 여러 번 귀하의 질문에 관련 키워드를 썼습니다 (심지어 원래 언어로). 마음에 위의 베어링
, 프로그램의 스케치는 다음과 같이 보일 수 있습니다 :
#!/usr/bin/python
# Command processing functions:
def func1():
return "Response 1"
def func2():
return "Response 2"
# Commands dictionary:
d = {"cmd1":func1, "cmd2":func2}
# Suppose this command was receiced by the bot:
command_received = "cmd1"
# Processing:
try:
response = d[command_received]()
except KeyError:
response = "Unknown command"
# Sending response:
print response
나는 [장고] 같은 파이썬의 서버 측 프레임 워크에서 라우터 구현 될 것입니다 좋은 예를 추가하고 싶습니다 (HTTP : //www.django-rest-framework.org/api-guide/routers/). –