Golang Equivalent of `is` Operator in Python

huangapple go评论83阅读模式
英文:

Golang Equivalent of `is` Operator in Python

问题

我是一名Python开发者,正在学习Go语言,并且正在编写一个简单的单链表实现作为练习。几年前我在Python中完成过这个任务,现在我想在Go中重新实现一遍。

在这个任务中有一个方法叫做remove(node),用于从链表中移除给定的节点。在Python中,我使用is运算符来实现。代码如下:

def remove(self, node):
    element = self.head
    prev = None
    while element:
        if element is node:
            # 从链表中移除节点...
        prev = element
        element = element.next

在Python中,is运算符用于检查对象的身份。例如:

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

尽管foobar的值相同,但它们并不是同一个对象。然而,foobaz是同一个对象。

那么在Go中如何实现相同的功能呢?在Go中,相等运算符==只检查值是否相同。例如:

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)
}

输出结果为:

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

在Go中,ab的值相同,但它们不是同一个对象。是否有一种类似Python的身份检查方式呢?或者是否有相关的包或者自定义函数可以实现身份检查呢?

英文:

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?

答案1

得分: 5

你所谈论的内容需要在Go语言中使用指针。在你的Python代码中,foo、bar和baz包含对对象的引用,因此你可以讨论其中两个是否引用同一个底层对象。在你的Go代码中,a、b和c是Test类型的变量。如果你将它们声明为Test的指针(*Test),你会看到不同的结果。试试这个:

package main

import "fmt"

type Test struct {
    x int
}

func main() {
    // a、b和c是Test类型的指针
    a := &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指向的对象的值相同
}
英文:

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
}

huangapple
  • 本文由 发表于 2016年2月22日 12:57:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/35546054.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定