我写了一个链表,当我使用多个赋值时,结果很奇怪。在python和javascript之间有什么不同?
示例python代码
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
node=ListNode(0)
node=node.next=ListNode(10)
#expect node.next=ListNode(10) node=node.next
print(node ==node.next) # True why?
相同的逻辑JavaScript代码
function ListNode(val){
this.val=val;
this.next=null;
}
var node=new ListNode(0);
node=node.next=new ListNode(10)
console.log(node==node.next) //false
答案 0 :(得分:0)
实际上,您重新分配{
request: {
method: 'GET',
url: '//some-route',
header: {accept: '*/*' }
}
等于server {
listen 80;
listen [::]:80 default_server ipv6only=on;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name myapi.com;
ssl on;
# Use certificate and key provided by Let's Encrypt:
ssl_certificate /etc/letsencrypt/live/myapi.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapi.com/privkey.pem;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location /api {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
,因此node
始终为node.next
,因为这两个变量指向同一个对象。
答案 1 :(得分:0)
在Python中,评估的顺序相反。因此:
node = node.next = ListNode(10)
与此相同:
node = ListNode(10)
node.next = node
因此,您必须在最后一次分配之前颠倒元素的顺序:
node.next = node = ListNode(10)
# same as:
node.next = ListNode(10)
node = node.next
答案 2 :(得分:0)
我不确定你要用这条线完成什么:
node = node.next = ListNode(10)
如果您这样做:
node.next = ListNode(10)
您将获得所需的结果
in [1]: node=ListNode(0)
In [2]: node.val
Out[2]: 0
In [3]: node.next = ListNode(10)
In [4]: node.val
Out[4]: 0
In [5]: node.next.val
Out[5]: 10
In [6]: node == node.next
Out[6]: False
答案 3 :(得分:-1)
从右到左评估分配操作:
node.next = ListNode(10)
node = node.next