简体   繁体   English

为什么此Python类具有不同的地址?

[英]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.

相关问题 为什么不同数据类型的地址不同[Python]? - Why the addresses of different datatypes different [Python]? 为什么python方法存储在不同的地址中? - Why python methods are stored in different addresses? 为什么SimpleNamespace的大小与空类的大小不同? - Why does SimpleNamespace have a different size than that of an empty class? 了解为什么Bitcoinlib生成的地址与我拥有的地址不同 - Understand why Bitcoinlib is generating different addresses than what I have 为什么相同 class 的两个实例具有不同的属性(Python)是明智的? - Why is it sensible for two instances of the same class to have different attributes (Python)? 为什么接近零的除法在python中有不同的行为? - Why does division near to zero have different behaviors in python? 为什么Python将类中的self视为我必须给出的参数 - Why does Python consider the self in a class as an argument I have to give 为什么python datetime类有&#39;fromtimestamp&#39;方法,但不是&#39;totimestamp&#39;方法? - Why does the python datetime class have a 'fromtimestamp' method, but not a 'totimestamp' method? 为什么我的Python类声称我有2个参数而不是1? - Why does my Python class claim that I have 2 arguments instead of 1? 为什么python允许为同一个类创建不同的属性集? - Why does python allow to make different set of attributes for same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM