Python运算符优先级-2 ** 3 ** 2 ** 1 = 512-不知道如何?

时间:2018-09-18 04:13:15

标签: python python-3.x

我是Python的新手。有人可以解释一下为什么将2 ** 3 ** 2 ** 1评估为512。在这里我了解PEMDAS和从左到右的处理方式,但无法确定产生此输出的原因。非常感谢。

2 个答案:

答案 0 :(得分:1)

除幂运算符外,Python运算符通常从左到右求值:

  

同一框中的操作员从左到右分组(除   取幂,从从右到左分组。

Source

因此

 2 ** 3 ** 2 ** 1 

相同
 2 ** (3 ** (2 ** 1))

答案 1 :(得分:0)

我自己不是Python开发人员,所以如果我错了,其他人可以纠正我,但是我敢猜测这是如何解析的:

export const usersFetchData = (url) => {
 return (dispatch) => {
  dispatch(userIsLoading(true));

  axios
    .get(url)
    .then(res => {
        if(!res.ok){
            throw Error(res.statusText)
        }
        dispatch(userIsLoading(false));
        console.log(res.data);
        return res.data; // returning the json respone    
    })
    //.then(res => res.json()) // removed this, you don't need it
    .then(users => {
        console.log(users);
        dispatch(usersFetchDataSuccess(users))
    })
    .catch(() => dispatch(userHasErrored(true)));
 }
}
相关问题