Golang在Python中是```运算符的等价物 [英] Golang Equivalent of `is` Operator in Python

查看:148
本文介绍了Golang在Python中是```运算符的等价物的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名正在学习Go的Python开发人员,正在编写一个简单的单链表实现作为练习。几年前,我在Python中完成了这个工作,现在正在使用Go进行复制。



分配中的一种方法(我最初在学校做过)是 remove(node):remove给定节点形成列表。在Python中,我使用操作符。就像这样:

  def remove(self,node):
element = self.head
prev = None
while元素:
如果元素为节点:$ b​​ $ b删除节点表单...
prev =元素
元素= element.next

在Python中,操作符检查标识。因此,例如

 >>>类Foo(object):
... def __init __(self,x):
... self.x = x
...
>>> foo = Foo(5)
>>> bar = Foo(5)
>>> baz = foo
>>> foo是baz
True
>>> foo是bar
False

即使实例 foo bar 是相同的,它们不是同一个对象,正如我们在这里看到的那样:

 >>> id(foo)
139725093837712
>>> id(bar)
139725093837904

然而 foo baz 是同一个对象:

 >> ;> id(foo)
139725093837712
>>> id(baz)
139725093837712

如何在Go中执行相同的操作?等号运算符 == 只是检查值是否相同:

  b 







$ b func main(






$ ){
a:= Test {5}
b:= Test {5}
c:= Test {6}

fmt.Println(a == b ,a == b)
fmt.Println(a == c,a == c)
fmt.Println(b == c,a == c)
}

输出:

  a == b true 
a == c false
b == c false

游乐场链接



a b 具有相同的值,但不是同一个对象。 Go有没有类似Python的方法来检查身份?或者是有没有一个可用的包或者一些方式来自己推出身份检查功能? 解决方案

约需要在Go中使用指针。在您的Python代码中,foo,bar和baz包含对对象的引用,因此您可以讨论它们中的两个是否引用相同的基础对象。在你的Go代码中,a,b和c是Test类型的变量。如果你将它们声明为Test(* Test)的指针,你会看到不同的东西。试试这个:

  package main 

importfmt

type Test struct {
x int
}

func main(){
// a,b和c是类型为Test的指针b $ ba:=& amp ; Test {5}
b:=& Test {5}
c:= a

fmt.Println(a == b,a == b)// a和b指向不同的对象
fmt.Println(a == c,a == c)// a和c指向同一个对象
fmt.Println(* a == * b,* a == * b)//由a和b指向的对象的值是相同的
}


I'm a Python developer who is learning Go and am writing a simple singly linked list implementation as an exercise. I had done this a few years ago in Python and am duplicating now in Go.

One of the methods in the assignment (I did this initially in school) was remove(node): remove a given node form the list. In Python I did this using the is operator. Something like this:

def remove(self, node):
     element = self.head
     prev = None
     while element:
         if element is node:
             remove node form list...
         prev = element
         element = element.next

In Python the is operator checks identity. So for example

>>> class Foo(object):
...     def __init__(self, x):
...         self.x = x
... 
>>> foo = Foo(5)
>>> bar = Foo(5)
>>> baz = foo
>>> foo is baz
True
>>> foo is bar
False

Even though the values on the instances foo and bar are the same they are not the same object, as we see here:

>>> id(foo)
139725093837712
>>> id(bar)
139725093837904

However foo and baz are the same object:

>>> id(foo)
139725093837712
>>> id(baz)
139725093837712

How would I go about doing the same thing in Go? The equality operator, ==, just checks that the values are the same:

package main

import "fmt"

type Test struct {
    x int
}

func main() {
  a := Test{5}
  b := Test{5}
  c := Test{6}

  fmt.Println("a == b", a == b)
  fmt.Println("a == c ", a == c)
  fmt.Println("b == c ", a == c)
}

Which outputs:

a == b true
a == c  false
b == c  false

Playground link

a and b have the same value but are not the same object. Is there a way to check for identity in Go similar to Python? Or is is there a package available for it or some way to roll an identity checking function myself?

解决方案

What you're talking about would require the use of pointers in Go. In your Python code, foo, bar, and baz contain references to objects, so you can talk about whether two of them refer to the same underlying object. In your Go code, a, b, and c are variables of type Test. If you declared them as pointers to Test (*Test), you would see something different. Try this:

package main

import "fmt"

type Test struct {
    x int
}

func main() {
    // a, b, and c are pointers to type Test
    a := &Test{5}
    b := &Test{5}
    c := a

    fmt.Println("a == b", a == b)     // a and b point to different objects
    fmt.Println("a == c", a == c)     // a and c point to the same object
    fmt.Println("*a == *b", *a == *b) // The values of the objects pointed to by a and b are the same
}

这篇关于Golang在Python中是```运算符的等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆