[英]Why does this Python class have different addresses?
I made a simple class: 我做了一个简单的课:
class Foo:
pass
then I checked its address with id
: 然后我检查了
id
为的地址:
>>> id(Foo)
4299236488
As I was curious, I checked another way: 出于好奇,我检查了另一种方法:
>>> id(Foo())
4332721208
Why do they have two different addresses? 他们为什么有两个不同的地址?
Foo
is an object and Foo()
is an instance of the object Foo
. Foo
是一个对象,而Foo()
是该对象Foo
的实例。
>>> type(Foo)
<type 'classobj'>
>>> id(Foo)
140710195094936
>>> type(Foo())
<type 'instance'>
>>> id(Foo())
140710195200224
You did not check it in another way. 您没有以其他方式检查它。
When you call foo
you just ask where your class is located. 当您调用
foo
您只需问您的课程在哪里。
When you called foo()
you created an instance of you class. 调用
foo()
您创建了您的类的实例。 And then ask where your instance of your class is located. 然后问您的类的实例位于何处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.