Python 中 `is` 运算符的 Golang 等价物
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:
remove node form list...
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
尽管实例 foo
和 bar
上的值相同,但它们不是同一个对象,正如我们在此处看到的:
>>> id(foo)
139725093837712
>>> id(bar)
139725093837904
但是foo
和baz
是同一个对象:
>>> id(foo)
139725093837712
>>> id(baz)
139725093837712
我将如何在 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
a
和 b
具有相同的值但不是相同的对象。有没有一种方法可以在 Go 中检查身份,类似于 Python?或者是否有可用的软件包或某种方式可以自己滚动身份检查功能?
你所说的需要在 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, 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
}
我是一名 Python 开发人员,正在学习 Go 并且正在编写一个简单的单链表实现作为练习。几年前我在 Python 中做过这个,现在正在用 Go 复制。
作业中的方法之一(我最初在学校这样做)是remove(node)
:从列表中删除给定节点。在 Python 中,我使用 is
运算符来完成此操作。像这样:
def remove(self, node):
element = self.head
prev = None
while element:
if element is node:
remove node form list...
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
尽管实例 foo
和 bar
上的值相同,但它们不是同一个对象,正如我们在此处看到的:
>>> id(foo)
139725093837712
>>> id(bar)
139725093837904
但是foo
和baz
是同一个对象:
>>> id(foo)
139725093837712
>>> id(baz)
139725093837712
我将如何在 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
a
和 b
具有相同的值但不是相同的对象。有没有一种方法可以在 Go 中检查身份,类似于 Python?或者是否有可用的软件包或某种方式可以自己滚动身份检查功能?
你所说的需要在 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, 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
}