Python: SyntaxError: keyword can't be an expre

2019-01-25 02:30发布

In a Python script I call a function from rpy2, but I get this error:

#using an R module 
res = DirichletReg.ddirichlet(np.asarray(my_values),alphas,
                              log=False, sum.up=False) 
SyntaxError: keyword can't be an expression

What exactly went wrong here?

5条回答
Evening l夕情丶
2楼-- · 2019-01-25 02:43

I guess many of us who came to this page have a problem with Scikit Learn, one way to solve it is to create a dictionary with parameters and pass it to the model:

params = {'C': 1e9, 'gamma': 1e-07}
cls = SVC(**params)    
查看更多
放我归山
3楼-- · 2019-01-25 02:53

I just got that problem when converting from % formatting to .format().

Previous code:

"SET !TIMEOUT_STEP %{USER_TIMEOUT_STEP}d" % {'USER_TIMEOUT_STEP' = 3}

Problematic syntax:

"SET !TIMEOUT_STEP {USER_TIMEOUT_STEP}".format('USER_TIMEOUT_STEP' = 3)

The problem is that format is a function that needs parameters. They cannot be strings. That is one of worst python error messages I've ever seen.

Corrected code:

"SET !TIMEOUT_STEP {USER_TIMEOUT_STEP}".format(USER_TIMEOUT_STEP = 3)
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-25 03:04

sum.up is not a valid keyword argument name. Keyword arguments must be valid identifiers. You should look in the documentation of the library you are using how this argument really is called – maybe sum_up?

查看更多
Summer. ? 凉城
5楼-- · 2019-01-25 03:04

It's python source parser failure on sum.up=False named argument as sum.up is not valid argument name (you can't use dots -- only alphanumerics and underscores in argument names).

查看更多
老娘就宠你
6楼-- · 2019-01-25 03:07

Using the Elastic search DSL API, you may hit the same error with

s = Search(using=client, index="my-index") \
    .query("match", category.keyword="Musician")

You can solve it by doing:

s = Search(using=client, index="my-index") \
    .query({"match": {"category.keyword":"Musician/Band"}})
查看更多
登录 后发表回答