我不认为有一种方法可以让IDE直接从一个链接到另一个,而你的代码看起来和现在一样,而不需要编写自己的插件,我不建议这样做。但是,有一些方法可以更改代码,这可能会对您有所帮助。
选项1:链接到通用定义
endpoint1 = 'endpoint1'
,然后使用
send({'command': endpoint1})
和
if case(endpoint1):
选项2:使用继承而不是“switch”语句定义逻辑
from abc import ABC, abstractmethod
class AbstractEndpoint(ABC):
def do_stuff(self):
# default
pass
@abstractmethod
def serialize(self):
pass
class Endpoint1(AbstractEndpoint):
def do_stuff(self):
print('do something')
def serialize(self):
return 'endpoint1'
class Endpoint2(AbstractEndpoint):
def do_stuff(self):
print('do something else')
def serialize(self):
return 'endpoint2'
class Endpoint3(AbstractEndpoint):
# does not override do_stuff, so default behaviour is used
def serialize(self):
return 'endpoint3'
deserialize_dict = {'endpoint1': Endpoint1, 'endpoint2': Endpoint2, 'endpoint3': Endpoint3}
def deserialize(string_repr):
return deserialize_dict[string_repr]()
e = Endpoint1()
# prints 'do something'
e.do_stuff()
# convert to and from string
e_serialize = e.serialize()
print(deserialize(e_serialize))
# does nothing because it doesn't override default behaviour
Endpoint3().do_stuff()
然后修改send函数以序列化对象,这样就可以编写
send({'command': Endpoint1()})