python-类似于 coffeescript 中的字符串插值

python-like interpolation of string in coffeescript

我有这样的命名空间有错误

errorMessages=
  error404Find: "Hosts not found"
  error404FindByHost: "Host - #{hostName} - not found"
  error400: "No host"

hostName 应分配给 null 并在函数中覆盖,如:

this.hostName = 'smth'

或者我可以使用类似 python 的东西:

errorMessages=
  {'error404Find': "Hosts not found"
  'error404FindByHost': "Host - {hostName} - not found"
  'error400': "No host"}
errorMessages['error404FindByHost'].format(hostName='smth')

您可以将所有这些条目设为一个函数,然后使用参数对它们应用字典:

errorMessages=
  error404Find:-> "Hosts not found"
  error404FindByHost:-> "Host - #{@hostName} - not found"
  error400:-> "No host"

alert errorMessages["error404FindByHost"].apply hostName:"host1"